1/*
2/*
3/*Copyright (C) 2015 The Android Open Source Project
4/*
5/*Licensed under the Apache License, Version 2.0 (the "License");
6/*you may not use this file except in compliance with the License.
7/*You may obtain a copy of the License at
8/*
9/*     http://www.apache.org/licenses/LICENSE-2.0
10/*
11/*Unless required by applicable law or agreed to in writing, software
12/*distributed under the License is distributed on an "AS IS" BASIS,
13/*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14/*See the License for the specific language governing permissions and
15/*limitations under the License.
16 */
17 *
18 * This file was copied from https://github.com/devttys0/libmpsse.git (sha1
19 * f1a6744b), and modified to suite the Chromium OS project.
20 *
21 * Internal functions used by libmpsse.
22 *
23 * Craig Heffner
24 * 27 December 2011
25 */
26#include <string.h>
27
28#include "trunks/ftdi/support.h"
29
30/* Write data to the FTDI chip */
31int raw_write(struct mpsse_context* mpsse, uint8_t* buf, int size) {
32   int retval = MPSSE_FAIL;
33
34   if (mpsse->mode) {
35     if (ftdi_write_data(&mpsse->ftdi, buf, size) == size) {
36       retval = MPSSE_OK;
37     }
38   }
39
40   return retval;
41 }
42
43 /* Read data from the FTDI chip */
44 int raw_read(struct mpsse_context* mpsse, uint8_t* buf, int size) {
45   int n = 0, r = 0;
46
47   if (mpsse->mode) {
48     while (n < size) {
49       r = ftdi_read_data(&mpsse->ftdi, buf, size);
50       if (r < 0)
51         break;
52       n += r;
53     }
54
55     if (mpsse->flush_after_read) {
56       /*
57        * Make sure the buffers are cleared after a read or subsequent reads may
58        *fail.
59        *
60        * Is this needed anymore? It slows down repetitive read operations by
61        *~8%.
62        */
63       ftdi_usb_purge_rx_buffer(&mpsse->ftdi);
64     }
65   }
66
67   return n;
68 }
69
70 /* Sets the read and write timeout periods for bulk usb data transfers. */
71 void set_timeouts(struct mpsse_context* mpsse, int timeout) {
72   if (mpsse->mode) {
73     mpsse->ftdi.usb_read_timeout = timeout;
74     mpsse->ftdi.usb_write_timeout = timeout;
75   }
76
77   return;
78 }
79
80 /* Convert a frequency to a clock divisor */
81 uint16_t freq2div(uint32_t system_clock, uint32_t freq) {
82   return (((system_clock / freq) / 2) - 1);
83 }
84
85 /* Convert a clock divisor to a frequency */
86 uint32_t div2freq(uint32_t system_clock, uint16_t div) {
87   return (system_clock / ((1 + div) * 2));
88 }
89
90 /* Builds a buffer of commands + data blocks */
91 uint8_t* build_block_buffer(struct mpsse_context* mpsse,
92                             uint8_t cmd,
93                             const uint8_t* data,
94                             int size,
95                             int* buf_size) {
96   uint8_t* buf = NULL;
97   int i = 0, j = 0, k = 0, dsize = 0, num_blocks = 0, total_size = 0,
98       xfer_size = 0;
99   uint16_t rsize = 0;
100
101   *buf_size = 0;
102
103   /* Data block size is 1 in I2C, or when in bitmode */
104   if (mpsse->mode == I2C || (cmd & MPSSE_BITMODE)) {
105     xfer_size = 1;
106   } else {
107     xfer_size = mpsse->xsize;
108   }
109
110   num_blocks = (size / xfer_size);
111   if (size % xfer_size) {
112     num_blocks++;
113   }
114
115   /* The total size of the data will be the data size + the write command */
116   total_size = size + (CMD_SIZE * num_blocks);
117
118   /* In I2C we have to add 3 additional commands per data block */
119   if (mpsse->mode == I2C) {
120     total_size += (CMD_SIZE * 3 * num_blocks);
121   }
122
123   buf = malloc(total_size);
124   if (buf) {
125     memset(buf, 0, total_size);
126
127     for (j = 0; j < num_blocks; j++) {
128       dsize = size - k;
129       if (dsize > xfer_size) {
130         dsize = xfer_size;
131       }
132
133       /* The reported size of this block is block size - 1 */
134       rsize = dsize - 1;
135
136       /* For I2C we need to ensure that the clock pin is set low prior to
137        * clocking out data */
138       if (mpsse->mode == I2C) {
139         buf[i++] = SET_BITS_LOW;
140         buf[i++] = mpsse->pstart & ~SK;
141
142         /* On receive, we need to ensure that the data out line is set as an
143          * input to avoid contention on the bus */
144         if (cmd == mpsse->rx) {
145           buf[i++] = mpsse->tris & ~DO;
146         } else {
147           buf[i++] = mpsse->tris;
148         }
149       }
150
151       /* Copy in the command for this block */
152       buf[i++] = cmd;
153       buf[i++] = (rsize & 0xFF);
154       if (!(cmd & MPSSE_BITMODE)) {
155         buf[i++] = ((rsize >> 8) & 0xFF);
156       }
157
158       /* On a write, copy the data to transmit after the command */
159       if (cmd == mpsse->tx || cmd == mpsse->txrx) {
160         memcpy(buf + i, data + k, dsize);
161
162         /* i == offset into buf */
163         i += dsize;
164         /* k == offset into data */
165         k += dsize;
166       }
167
168       /* In I2C mode we need to clock one ACK bit after each byte */
169       if (mpsse->mode == I2C) {
170         /* If we are receiving data, then we need to clock out an ACK for each
171          * byte */
172         if (cmd == mpsse->rx) {
173           buf[i++] = SET_BITS_LOW;
174           buf[i++] = mpsse->pstart & ~SK;
175           buf[i++] = mpsse->tris;
176
177           buf[i++] = mpsse->tx | MPSSE_BITMODE;
178           buf[i++] = 0;
179           buf[i++] = mpsse->tack;
180         }
181         /* If we are sending data, then we need to clock in an ACK for each
182          * byte
183            */
184         else if (cmd == mpsse->tx) {
185           /* Need to make data out an input to avoid contention on the bus when
186            * the slave sends an ACK */
187           buf[i++] = SET_BITS_LOW;
188           buf[i++] = mpsse->pstart & ~SK;
189           buf[i++] = mpsse->tris & ~DO;
190
191           buf[i++] = mpsse->rx | MPSSE_BITMODE;
192           buf[i++] = 0;
193           buf[i++] = SEND_IMMEDIATE;
194         }
195       }
196     }
197
198     *buf_size = i;
199   }
200
201   return buf;
202 }
203
204 /* Set the low bit pins high/low */
205 int set_bits_low(struct mpsse_context* mpsse, int port) {
206   char buf[CMD_SIZE] = {0};
207
208   buf[0] = SET_BITS_LOW;
209   buf[1] = port;
210   buf[2] = mpsse->tris;
211
212   return raw_write(mpsse, (uint8_t*)&buf, sizeof(buf));
213 }
214
215 /* Set the high bit pins high/low */
216 int set_bits_high(struct mpsse_context* mpsse, int port) {
217   char buf[CMD_SIZE] = {0};
218
219   buf[0] = SET_BITS_HIGH;
220   buf[1] = port;
221   buf[2] = mpsse->trish;
222
223   return raw_write(mpsse, (uint8_t*)&buf, sizeof(buf));
224 }
225
226 /* Set the GPIO pins high/low */
227 int gpio_write(struct mpsse_context* mpsse, int pin, int direction) {
228   int retval = MPSSE_FAIL;
229
230   if (mpsse->mode == BITBANG) {
231     if (direction == HIGH) {
232       mpsse->bitbang |= (1 << pin);
233     } else {
234       mpsse->bitbang &= ~(1 << pin);
235     }
236
237     if (set_bits_high(mpsse, mpsse->bitbang) == MPSSE_OK) {
238       retval = raw_write(mpsse, (uint8_t*)&mpsse->bitbang, 1);
239     }
240   } else {
241     /* The first four pins can't be changed unless we are in a stopped status
242      */
243     if (pin < NUM_GPIOL_PINS && mpsse->status == STOPPED) {
244       /* Convert pin number (0-3) to the corresponding pin bit */
245       pin = (GPIO0 << pin);
246
247       if (direction == HIGH) {
248         mpsse->pstart |= pin;
249         mpsse->pidle |= pin;
250         mpsse->pstop |= pin;
251       } else {
252         mpsse->pstart &= ~pin;
253         mpsse->pidle &= ~pin;
254         mpsse->pstop &= ~pin;
255       }
256
257       retval = set_bits_low(mpsse, mpsse->pstop);
258     } else if (pin >= NUM_GPIOL_PINS && pin < NUM_GPIO_PINS) {
259       /* Convert pin number (4 - 11) to the corresponding pin bit */
260       pin -= NUM_GPIOL_PINS;
261
262       if (direction == HIGH) {
263         mpsse->gpioh |= (1 << pin);
264       } else {
265         mpsse->gpioh &= ~(1 << pin);
266       }
267
268       retval = set_bits_high(mpsse, mpsse->gpioh);
269     }
270   }
271
272   return retval;
273 }
274
275 /* Checks if a given MPSSE context is valid. */
276 int is_valid_context(struct mpsse_context* mpsse) {
277   return mpsse != NULL;
278 }
279