Lines Matching +full:1 +full:ms

36 #define SPI_CTRL1_SPIE		(1 << 7)
37 #define SPI_CTRL1_SPE (1 << 6)
38 #define SPI_CTRL1_MSTR (1 << 4)
39 #define SPI_CTRL1_CPOL (1 << 3)
40 #define SPI_CTRL1_CPHA (1 << 2)
41 #define SPI_CTRL1_SSOE (1 << 1)
42 #define SPI_CTRL1_LSBFE (1 << 0)
48 #define SPI_STATUS_SPIF (1 << 7)
49 #define SPI_STATUS_WCOL (1 << 6)
50 #define SPI_STATUS_MODF (1 << 4)
60 #define FSM_POLL 1 /* need to poll for completion, an IRQ is */
87 int (*state)(int irq, struct mpc52xx_spi *ms, u8 status, u8 data);
100 static void mpc52xx_spi_chipsel(struct mpc52xx_spi *ms, int value) in mpc52xx_spi_chipsel() argument
104 if (ms->gpio_cs_count > 0) { in mpc52xx_spi_chipsel()
105 cs = spi_get_chipselect(ms->message->spi, 0); in mpc52xx_spi_chipsel()
106 gpiod_set_value(ms->gpio_cs[cs], value); in mpc52xx_spi_chipsel()
108 out_8(ms->regs + SPI_PORTDATA, value ? 0 : 0x08); in mpc52xx_spi_chipsel()
117 static void mpc52xx_spi_start_transfer(struct mpc52xx_spi *ms) in mpc52xx_spi_start_transfer() argument
119 ms->rx_buf = ms->transfer->rx_buf; in mpc52xx_spi_start_transfer()
120 ms->tx_buf = ms->transfer->tx_buf; in mpc52xx_spi_start_transfer()
121 ms->len = ms->transfer->len; in mpc52xx_spi_start_transfer()
124 if (ms->cs_change) in mpc52xx_spi_start_transfer()
125 mpc52xx_spi_chipsel(ms, 1); in mpc52xx_spi_start_transfer()
126 ms->cs_change = ms->transfer->cs_change; in mpc52xx_spi_start_transfer()
129 ms->wcol_tx_timestamp = mftb(); in mpc52xx_spi_start_transfer()
130 if (ms->tx_buf) in mpc52xx_spi_start_transfer()
131 out_8(ms->regs + SPI_DATA, *ms->tx_buf++); in mpc52xx_spi_start_transfer()
133 out_8(ms->regs + SPI_DATA, 0); in mpc52xx_spi_start_transfer()
137 static int mpc52xx_spi_fsmstate_transfer(int irq, struct mpc52xx_spi *ms,
139 static int mpc52xx_spi_fsmstate_wait(int irq, struct mpc52xx_spi *ms,
149 mpc52xx_spi_fsmstate_idle(int irq, struct mpc52xx_spi *ms, u8 status, u8 data) in mpc52xx_spi_fsmstate_idle() argument
156 dev_err(&ms->host->dev, "spurious irq, status=0x%.2x\n", in mpc52xx_spi_fsmstate_idle()
160 if (list_empty(&ms->queue)) in mpc52xx_spi_fsmstate_idle()
164 ms->message = list_first_entry(&ms->queue, struct spi_message, queue); in mpc52xx_spi_fsmstate_idle()
165 list_del_init(&ms->message->queue); in mpc52xx_spi_fsmstate_idle()
169 spi = ms->message->spi; in mpc52xx_spi_fsmstate_idle()
176 out_8(ms->regs + SPI_CTRL1, ctrl1); in mpc52xx_spi_fsmstate_idle()
179 /* minimum divider is '2'. Also, add '1' to force rounding the in mpc52xx_spi_fsmstate_idle()
181 sppr = ((ms->ipb_freq / ms->message->spi->max_speed_hz) + 1) >> 1; in mpc52xx_spi_fsmstate_idle()
183 if (sppr < 1) in mpc52xx_spi_fsmstate_idle()
184 sppr = 1; in mpc52xx_spi_fsmstate_idle()
185 while (((sppr - 1) & ~0x7) != 0) { in mpc52xx_spi_fsmstate_idle()
186 sppr = (sppr + 1) >> 1; /* add '1' to force rounding up */ in mpc52xx_spi_fsmstate_idle()
189 sppr--; /* sppr quantity in register is offset by 1 */ in mpc52xx_spi_fsmstate_idle()
195 out_8(ms->regs + SPI_BRR, sppr << 4 | spr); /* Set speed */ in mpc52xx_spi_fsmstate_idle()
197 ms->cs_change = 1; in mpc52xx_spi_fsmstate_idle()
198 ms->transfer = container_of(ms->message->transfers.next, in mpc52xx_spi_fsmstate_idle()
201 mpc52xx_spi_start_transfer(ms); in mpc52xx_spi_fsmstate_idle()
202 ms->state = mpc52xx_spi_fsmstate_transfer; in mpc52xx_spi_fsmstate_idle()
215 static int mpc52xx_spi_fsmstate_transfer(int irq, struct mpc52xx_spi *ms, in mpc52xx_spi_fsmstate_transfer() argument
219 return ms->irq0 ? FSM_STOP : FSM_POLL; in mpc52xx_spi_fsmstate_transfer()
229 ms->wcol_count++; in mpc52xx_spi_fsmstate_transfer()
230 ms->wcol_ticks += mftb() - ms->wcol_tx_timestamp; in mpc52xx_spi_fsmstate_transfer()
231 ms->wcol_tx_timestamp = mftb(); in mpc52xx_spi_fsmstate_transfer()
233 if (ms->tx_buf) in mpc52xx_spi_fsmstate_transfer()
234 data = *(ms->tx_buf - 1); in mpc52xx_spi_fsmstate_transfer()
235 out_8(ms->regs + SPI_DATA, data); /* try again */ in mpc52xx_spi_fsmstate_transfer()
238 ms->modf_count++; in mpc52xx_spi_fsmstate_transfer()
239 dev_err(&ms->host->dev, "mode fault\n"); in mpc52xx_spi_fsmstate_transfer()
240 mpc52xx_spi_chipsel(ms, 0); in mpc52xx_spi_fsmstate_transfer()
241 ms->message->status = -EIO; in mpc52xx_spi_fsmstate_transfer()
242 if (ms->message->complete) in mpc52xx_spi_fsmstate_transfer()
243 ms->message->complete(ms->message->context); in mpc52xx_spi_fsmstate_transfer()
244 ms->state = mpc52xx_spi_fsmstate_idle; in mpc52xx_spi_fsmstate_transfer()
249 ms->byte_count++; in mpc52xx_spi_fsmstate_transfer()
250 if (ms->rx_buf) in mpc52xx_spi_fsmstate_transfer()
251 *ms->rx_buf++ = data; in mpc52xx_spi_fsmstate_transfer()
254 ms->len--; in mpc52xx_spi_fsmstate_transfer()
255 if (ms->len == 0) { in mpc52xx_spi_fsmstate_transfer()
256 ms->timestamp = mftb(); in mpc52xx_spi_fsmstate_transfer()
257 if (ms->transfer->delay.unit == SPI_DELAY_UNIT_USECS) in mpc52xx_spi_fsmstate_transfer()
258 ms->timestamp += ms->transfer->delay.value * in mpc52xx_spi_fsmstate_transfer()
260 ms->state = mpc52xx_spi_fsmstate_wait; in mpc52xx_spi_fsmstate_transfer()
265 ms->wcol_tx_timestamp = mftb(); in mpc52xx_spi_fsmstate_transfer()
266 if (ms->tx_buf) in mpc52xx_spi_fsmstate_transfer()
267 out_8(ms->regs + SPI_DATA, *ms->tx_buf++); in mpc52xx_spi_fsmstate_transfer()
269 out_8(ms->regs + SPI_DATA, 0); in mpc52xx_spi_fsmstate_transfer()
281 mpc52xx_spi_fsmstate_wait(int irq, struct mpc52xx_spi *ms, u8 status, u8 data) in mpc52xx_spi_fsmstate_wait() argument
284 dev_err(&ms->host->dev, "spurious irq, status=0x%.2x\n", in mpc52xx_spi_fsmstate_wait()
287 if (((int)mftb()) - ms->timestamp < 0) in mpc52xx_spi_fsmstate_wait()
290 ms->message->actual_length += ms->transfer->len; in mpc52xx_spi_fsmstate_wait()
295 if (ms->transfer->transfer_list.next == &ms->message->transfers) { in mpc52xx_spi_fsmstate_wait()
296 ms->msg_count++; in mpc52xx_spi_fsmstate_wait()
297 mpc52xx_spi_chipsel(ms, 0); in mpc52xx_spi_fsmstate_wait()
298 ms->message->status = 0; in mpc52xx_spi_fsmstate_wait()
299 if (ms->message->complete) in mpc52xx_spi_fsmstate_wait()
300 ms->message->complete(ms->message->context); in mpc52xx_spi_fsmstate_wait()
301 ms->state = mpc52xx_spi_fsmstate_idle; in mpc52xx_spi_fsmstate_wait()
307 if (ms->cs_change) in mpc52xx_spi_fsmstate_wait()
308 mpc52xx_spi_chipsel(ms, 0); in mpc52xx_spi_fsmstate_wait()
310 ms->transfer = container_of(ms->transfer->transfer_list.next, in mpc52xx_spi_fsmstate_wait()
312 mpc52xx_spi_start_transfer(ms); in mpc52xx_spi_fsmstate_wait()
313 ms->state = mpc52xx_spi_fsmstate_transfer; in mpc52xx_spi_fsmstate_wait()
320 * @ms: pointer to mpc52xx_spi driver data
322 static void mpc52xx_spi_fsm_process(int irq, struct mpc52xx_spi *ms) in mpc52xx_spi_fsm_process() argument
330 status = in_8(ms->regs + SPI_STATUS); in mpc52xx_spi_fsm_process()
331 data = in_8(ms->regs + SPI_DATA); in mpc52xx_spi_fsm_process()
332 rc = ms->state(irq, ms, status, data); in mpc52xx_spi_fsm_process()
336 schedule_work(&ms->work); in mpc52xx_spi_fsm_process()
344 struct mpc52xx_spi *ms = _ms; in mpc52xx_spi_irq() local
345 spin_lock(&ms->lock); in mpc52xx_spi_irq()
346 mpc52xx_spi_fsm_process(irq, ms); in mpc52xx_spi_irq()
347 spin_unlock(&ms->lock); in mpc52xx_spi_irq()
356 struct mpc52xx_spi *ms = container_of(work, struct mpc52xx_spi, work); in mpc52xx_spi_wq() local
359 spin_lock_irqsave(&ms->lock, flags); in mpc52xx_spi_wq()
360 mpc52xx_spi_fsm_process(0, ms); in mpc52xx_spi_wq()
361 spin_unlock_irqrestore(&ms->lock, flags); in mpc52xx_spi_wq()
370 struct mpc52xx_spi *ms = spi_controller_get_devdata(spi->controller); in mpc52xx_spi_transfer() local
376 spin_lock_irqsave(&ms->lock, flags); in mpc52xx_spi_transfer()
377 list_add_tail(&m->queue, &ms->queue); in mpc52xx_spi_transfer()
378 spin_unlock_irqrestore(&ms->lock, flags); in mpc52xx_spi_transfer()
379 schedule_work(&ms->work); in mpc52xx_spi_transfer()
390 struct mpc52xx_spi *ms; in mpc52xx_spi_probe() local
424 host = spi_alloc_host(&op->dev, sizeof(*ms)); in mpc52xx_spi_probe()
437 ms = spi_controller_get_devdata(host); in mpc52xx_spi_probe()
438 ms->host = host; in mpc52xx_spi_probe()
439 ms->regs = regs; in mpc52xx_spi_probe()
440 ms->irq0 = irq_of_parse_and_map(op->dev.of_node, 0); in mpc52xx_spi_probe()
441 ms->irq1 = irq_of_parse_and_map(op->dev.of_node, 1); in mpc52xx_spi_probe()
442 ms->state = mpc52xx_spi_fsmstate_idle; in mpc52xx_spi_probe()
443 ms->ipb_freq = mpc5xxx_get_bus_frequency(&op->dev); in mpc52xx_spi_probe()
444 ms->gpio_cs_count = gpiod_count(&op->dev, NULL); in mpc52xx_spi_probe()
445 if (ms->gpio_cs_count > 0) { in mpc52xx_spi_probe()
446 host->num_chipselect = ms->gpio_cs_count; in mpc52xx_spi_probe()
447 ms->gpio_cs = kmalloc_array(ms->gpio_cs_count, in mpc52xx_spi_probe()
448 sizeof(*ms->gpio_cs), in mpc52xx_spi_probe()
450 if (!ms->gpio_cs) { in mpc52xx_spi_probe()
455 for (i = 0; i < ms->gpio_cs_count; i++) { in mpc52xx_spi_probe()
466 ms->gpio_cs[i] = gpio_cs; in mpc52xx_spi_probe()
470 spin_lock_init(&ms->lock); in mpc52xx_spi_probe()
471 INIT_LIST_HEAD(&ms->queue); in mpc52xx_spi_probe()
472 INIT_WORK(&ms->work, mpc52xx_spi_wq); in mpc52xx_spi_probe()
475 if (ms->irq0 && ms->irq1) { in mpc52xx_spi_probe()
476 rc = request_irq(ms->irq0, mpc52xx_spi_irq, 0, in mpc52xx_spi_probe()
477 "mpc5200-spi-modf", ms); in mpc52xx_spi_probe()
478 rc |= request_irq(ms->irq1, mpc52xx_spi_irq, 0, in mpc52xx_spi_probe()
479 "mpc5200-spi-spif", ms); in mpc52xx_spi_probe()
481 free_irq(ms->irq0, ms); in mpc52xx_spi_probe()
482 free_irq(ms->irq1, ms); in mpc52xx_spi_probe()
483 ms->irq0 = ms->irq1 = 0; in mpc52xx_spi_probe()
487 ms->irq0 = ms->irq1 = 0; in mpc52xx_spi_probe()
490 if (!ms->irq0) in mpc52xx_spi_probe()
498 dev_info(&ms->host->dev, "registered MPC5200 SPI bus\n"); in mpc52xx_spi_probe()
503 dev_err(&ms->host->dev, "initialization failed\n"); in mpc52xx_spi_probe()
506 gpiod_put(ms->gpio_cs[i]); in mpc52xx_spi_probe()
508 kfree(ms->gpio_cs); in mpc52xx_spi_probe()
520 struct mpc52xx_spi *ms = spi_controller_get_devdata(host); in mpc52xx_spi_remove() local
523 free_irq(ms->irq0, ms); in mpc52xx_spi_remove()
524 free_irq(ms->irq1, ms); in mpc52xx_spi_remove()
526 for (i = 0; i < ms->gpio_cs_count; i++) in mpc52xx_spi_remove()
527 gpiod_put(ms->gpio_cs[i]); in mpc52xx_spi_remove()
529 kfree(ms->gpio_cs); in mpc52xx_spi_remove()
531 iounmap(ms->regs); in mpc52xx_spi_remove()