spi-bfin-sport.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*
  2. * SPI bus via the Blackfin SPORT peripheral
  3. *
  4. * Enter bugs at http://blackfin.uclinux.org/
  5. *
  6. * Copyright 2009-2011 Analog Devices Inc.
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/gpio.h>
  14. #include <linux/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/irq.h>
  17. #include <linux/errno.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/workqueue.h>
  22. #include <asm/portmux.h>
  23. #include <asm/bfin5xx_spi.h>
  24. #include <asm/blackfin.h>
  25. #include <asm/bfin_sport.h>
  26. #include <asm/cacheflush.h>
  27. #define DRV_NAME "bfin-sport-spi"
  28. #define DRV_DESC "SPI bus via the Blackfin SPORT"
  29. MODULE_AUTHOR("Cliff Cai");
  30. MODULE_DESCRIPTION(DRV_DESC);
  31. MODULE_LICENSE("GPL");
  32. MODULE_ALIAS("platform:bfin-sport-spi");
  33. enum bfin_sport_spi_state {
  34. START_STATE,
  35. RUNNING_STATE,
  36. DONE_STATE,
  37. ERROR_STATE,
  38. };
  39. struct bfin_sport_spi_master_data;
  40. struct bfin_sport_transfer_ops {
  41. void (*write) (struct bfin_sport_spi_master_data *);
  42. void (*read) (struct bfin_sport_spi_master_data *);
  43. void (*duplex) (struct bfin_sport_spi_master_data *);
  44. };
  45. struct bfin_sport_spi_master_data {
  46. /* Driver model hookup */
  47. struct device *dev;
  48. /* SPI framework hookup */
  49. struct spi_master *master;
  50. /* Regs base of SPI controller */
  51. struct sport_register __iomem *regs;
  52. int err_irq;
  53. /* Pin request list */
  54. u16 *pin_req;
  55. /* Driver message queue */
  56. struct workqueue_struct *workqueue;
  57. struct work_struct pump_messages;
  58. spinlock_t lock;
  59. struct list_head queue;
  60. int busy;
  61. bool run;
  62. /* Message Transfer pump */
  63. struct tasklet_struct pump_transfers;
  64. /* Current message transfer state info */
  65. enum bfin_sport_spi_state state;
  66. struct spi_message *cur_msg;
  67. struct spi_transfer *cur_transfer;
  68. struct bfin_sport_spi_slave_data *cur_chip;
  69. union {
  70. void *tx;
  71. u8 *tx8;
  72. u16 *tx16;
  73. };
  74. void *tx_end;
  75. union {
  76. void *rx;
  77. u8 *rx8;
  78. u16 *rx16;
  79. };
  80. void *rx_end;
  81. int cs_change;
  82. struct bfin_sport_transfer_ops *ops;
  83. };
  84. struct bfin_sport_spi_slave_data {
  85. u16 ctl_reg;
  86. u16 baud;
  87. u16 cs_chg_udelay; /* Some devices require > 255usec delay */
  88. u32 cs_gpio;
  89. u16 idle_tx_val;
  90. struct bfin_sport_transfer_ops *ops;
  91. };
  92. static void
  93. bfin_sport_spi_enable(struct bfin_sport_spi_master_data *drv_data)
  94. {
  95. bfin_write_or(&drv_data->regs->tcr1, TSPEN);
  96. bfin_write_or(&drv_data->regs->rcr1, TSPEN);
  97. SSYNC();
  98. }
  99. static void
  100. bfin_sport_spi_disable(struct bfin_sport_spi_master_data *drv_data)
  101. {
  102. bfin_write_and(&drv_data->regs->tcr1, ~TSPEN);
  103. bfin_write_and(&drv_data->regs->rcr1, ~TSPEN);
  104. SSYNC();
  105. }
  106. /* Caculate the SPI_BAUD register value based on input HZ */
  107. static u16
  108. bfin_sport_hz_to_spi_baud(u32 speed_hz)
  109. {
  110. u_long clk, sclk = get_sclk();
  111. int div = (sclk / (2 * speed_hz)) - 1;
  112. if (div < 0)
  113. div = 0;
  114. clk = sclk / (2 * (div + 1));
  115. if (clk > speed_hz)
  116. div++;
  117. return div;
  118. }
  119. /* Chip select operation functions for cs_change flag */
  120. static void
  121. bfin_sport_spi_cs_active(struct bfin_sport_spi_slave_data *chip)
  122. {
  123. gpio_direction_output(chip->cs_gpio, 0);
  124. }
  125. static void
  126. bfin_sport_spi_cs_deactive(struct bfin_sport_spi_slave_data *chip)
  127. {
  128. gpio_direction_output(chip->cs_gpio, 1);
  129. /* Move delay here for consistency */
  130. if (chip->cs_chg_udelay)
  131. udelay(chip->cs_chg_udelay);
  132. }
  133. static void
  134. bfin_sport_spi_stat_poll_complete(struct bfin_sport_spi_master_data *drv_data)
  135. {
  136. unsigned long timeout = jiffies + HZ;
  137. while (!(bfin_read(&drv_data->regs->stat) & RXNE)) {
  138. if (!time_before(jiffies, timeout))
  139. break;
  140. }
  141. }
  142. static void
  143. bfin_sport_spi_u8_writer(struct bfin_sport_spi_master_data *drv_data)
  144. {
  145. u16 dummy;
  146. while (drv_data->tx < drv_data->tx_end) {
  147. bfin_write(&drv_data->regs->tx16, *drv_data->tx8++);
  148. bfin_sport_spi_stat_poll_complete(drv_data);
  149. dummy = bfin_read(&drv_data->regs->rx16);
  150. }
  151. }
  152. static void
  153. bfin_sport_spi_u8_reader(struct bfin_sport_spi_master_data *drv_data)
  154. {
  155. u16 tx_val = drv_data->cur_chip->idle_tx_val;
  156. while (drv_data->rx < drv_data->rx_end) {
  157. bfin_write(&drv_data->regs->tx16, tx_val);
  158. bfin_sport_spi_stat_poll_complete(drv_data);
  159. *drv_data->rx8++ = bfin_read(&drv_data->regs->rx16);
  160. }
  161. }
  162. static void
  163. bfin_sport_spi_u8_duplex(struct bfin_sport_spi_master_data *drv_data)
  164. {
  165. while (drv_data->rx < drv_data->rx_end) {
  166. bfin_write(&drv_data->regs->tx16, *drv_data->tx8++);
  167. bfin_sport_spi_stat_poll_complete(drv_data);
  168. *drv_data->rx8++ = bfin_read(&drv_data->regs->rx16);
  169. }
  170. }
  171. static struct bfin_sport_transfer_ops bfin_sport_transfer_ops_u8 = {
  172. .write = bfin_sport_spi_u8_writer,
  173. .read = bfin_sport_spi_u8_reader,
  174. .duplex = bfin_sport_spi_u8_duplex,
  175. };
  176. static void
  177. bfin_sport_spi_u16_writer(struct bfin_sport_spi_master_data *drv_data)
  178. {
  179. u16 dummy;
  180. while (drv_data->tx < drv_data->tx_end) {
  181. bfin_write(&drv_data->regs->tx16, *drv_data->tx16++);
  182. bfin_sport_spi_stat_poll_complete(drv_data);
  183. dummy = bfin_read(&drv_data->regs->rx16);
  184. }
  185. }
  186. static void
  187. bfin_sport_spi_u16_reader(struct bfin_sport_spi_master_data *drv_data)
  188. {
  189. u16 tx_val = drv_data->cur_chip->idle_tx_val;
  190. while (drv_data->rx < drv_data->rx_end) {
  191. bfin_write(&drv_data->regs->tx16, tx_val);
  192. bfin_sport_spi_stat_poll_complete(drv_data);
  193. *drv_data->rx16++ = bfin_read(&drv_data->regs->rx16);
  194. }
  195. }
  196. static void
  197. bfin_sport_spi_u16_duplex(struct bfin_sport_spi_master_data *drv_data)
  198. {
  199. while (drv_data->rx < drv_data->rx_end) {
  200. bfin_write(&drv_data->regs->tx16, *drv_data->tx16++);
  201. bfin_sport_spi_stat_poll_complete(drv_data);
  202. *drv_data->rx16++ = bfin_read(&drv_data->regs->rx16);
  203. }
  204. }
  205. static struct bfin_sport_transfer_ops bfin_sport_transfer_ops_u16 = {
  206. .write = bfin_sport_spi_u16_writer,
  207. .read = bfin_sport_spi_u16_reader,
  208. .duplex = bfin_sport_spi_u16_duplex,
  209. };
  210. /* stop controller and re-config current chip */
  211. static void
  212. bfin_sport_spi_restore_state(struct bfin_sport_spi_master_data *drv_data)
  213. {
  214. struct bfin_sport_spi_slave_data *chip = drv_data->cur_chip;
  215. bfin_sport_spi_disable(drv_data);
  216. dev_dbg(drv_data->dev, "restoring spi ctl state\n");
  217. bfin_write(&drv_data->regs->tcr1, chip->ctl_reg);
  218. bfin_write(&drv_data->regs->tclkdiv, chip->baud);
  219. SSYNC();
  220. bfin_write(&drv_data->regs->rcr1, chip->ctl_reg & ~(ITCLK | ITFS));
  221. SSYNC();
  222. bfin_sport_spi_cs_active(chip);
  223. }
  224. /* test if there is more transfer to be done */
  225. static enum bfin_sport_spi_state
  226. bfin_sport_spi_next_transfer(struct bfin_sport_spi_master_data *drv_data)
  227. {
  228. struct spi_message *msg = drv_data->cur_msg;
  229. struct spi_transfer *trans = drv_data->cur_transfer;
  230. /* Move to next transfer */
  231. if (trans->transfer_list.next != &msg->transfers) {
  232. drv_data->cur_transfer =
  233. list_entry(trans->transfer_list.next,
  234. struct spi_transfer, transfer_list);
  235. return RUNNING_STATE;
  236. }
  237. return DONE_STATE;
  238. }
  239. /*
  240. * caller already set message->status;
  241. * dma and pio irqs are blocked give finished message back
  242. */
  243. static void
  244. bfin_sport_spi_giveback(struct bfin_sport_spi_master_data *drv_data)
  245. {
  246. struct bfin_sport_spi_slave_data *chip = drv_data->cur_chip;
  247. unsigned long flags;
  248. struct spi_message *msg;
  249. spin_lock_irqsave(&drv_data->lock, flags);
  250. msg = drv_data->cur_msg;
  251. drv_data->state = START_STATE;
  252. drv_data->cur_msg = NULL;
  253. drv_data->cur_transfer = NULL;
  254. drv_data->cur_chip = NULL;
  255. queue_work(drv_data->workqueue, &drv_data->pump_messages);
  256. spin_unlock_irqrestore(&drv_data->lock, flags);
  257. if (!drv_data->cs_change)
  258. bfin_sport_spi_cs_deactive(chip);
  259. if (msg->complete)
  260. msg->complete(msg->context);
  261. }
  262. static irqreturn_t
  263. sport_err_handler(int irq, void *dev_id)
  264. {
  265. struct bfin_sport_spi_master_data *drv_data = dev_id;
  266. u16 status;
  267. dev_dbg(drv_data->dev, "%s enter\n", __func__);
  268. status = bfin_read(&drv_data->regs->stat) & (TOVF | TUVF | ROVF | RUVF);
  269. if (status) {
  270. bfin_write(&drv_data->regs->stat, status);
  271. SSYNC();
  272. bfin_sport_spi_disable(drv_data);
  273. dev_err(drv_data->dev, "status error:%s%s%s%s\n",
  274. status & TOVF ? " TOVF" : "",
  275. status & TUVF ? " TUVF" : "",
  276. status & ROVF ? " ROVF" : "",
  277. status & RUVF ? " RUVF" : "");
  278. }
  279. return IRQ_HANDLED;
  280. }
  281. static void
  282. bfin_sport_spi_pump_transfers(unsigned long data)
  283. {
  284. struct bfin_sport_spi_master_data *drv_data = (void *)data;
  285. struct spi_message *message = NULL;
  286. struct spi_transfer *transfer = NULL;
  287. struct spi_transfer *previous = NULL;
  288. struct bfin_sport_spi_slave_data *chip = NULL;
  289. unsigned int bits_per_word;
  290. u32 tranf_success = 1;
  291. u32 transfer_speed;
  292. u8 full_duplex = 0;
  293. /* Get current state information */
  294. message = drv_data->cur_msg;
  295. transfer = drv_data->cur_transfer;
  296. chip = drv_data->cur_chip;
  297. transfer_speed = bfin_sport_hz_to_spi_baud(transfer->speed_hz);
  298. bfin_write(&drv_data->regs->tclkdiv, transfer_speed);
  299. SSYNC();
  300. /*
  301. * if msg is error or done, report it back using complete() callback
  302. */
  303. /* Handle for abort */
  304. if (drv_data->state == ERROR_STATE) {
  305. dev_dbg(drv_data->dev, "transfer: we've hit an error\n");
  306. message->status = -EIO;
  307. bfin_sport_spi_giveback(drv_data);
  308. return;
  309. }
  310. /* Handle end of message */
  311. if (drv_data->state == DONE_STATE) {
  312. dev_dbg(drv_data->dev, "transfer: all done!\n");
  313. message->status = 0;
  314. bfin_sport_spi_giveback(drv_data);
  315. return;
  316. }
  317. /* Delay if requested at end of transfer */
  318. if (drv_data->state == RUNNING_STATE) {
  319. dev_dbg(drv_data->dev, "transfer: still running ...\n");
  320. previous = list_entry(transfer->transfer_list.prev,
  321. struct spi_transfer, transfer_list);
  322. if (previous->delay_usecs)
  323. udelay(previous->delay_usecs);
  324. }
  325. if (transfer->len == 0) {
  326. /* Move to next transfer of this msg */
  327. drv_data->state = bfin_sport_spi_next_transfer(drv_data);
  328. /* Schedule next transfer tasklet */
  329. tasklet_schedule(&drv_data->pump_transfers);
  330. }
  331. if (transfer->tx_buf != NULL) {
  332. drv_data->tx = (void *)transfer->tx_buf;
  333. drv_data->tx_end = drv_data->tx + transfer->len;
  334. dev_dbg(drv_data->dev, "tx_buf is %p, tx_end is %p\n",
  335. transfer->tx_buf, drv_data->tx_end);
  336. } else
  337. drv_data->tx = NULL;
  338. if (transfer->rx_buf != NULL) {
  339. full_duplex = transfer->tx_buf != NULL;
  340. drv_data->rx = transfer->rx_buf;
  341. drv_data->rx_end = drv_data->rx + transfer->len;
  342. dev_dbg(drv_data->dev, "rx_buf is %p, rx_end is %p\n",
  343. transfer->rx_buf, drv_data->rx_end);
  344. } else
  345. drv_data->rx = NULL;
  346. drv_data->cs_change = transfer->cs_change;
  347. /* Bits per word setup */
  348. bits_per_word = transfer->bits_per_word;
  349. if (bits_per_word == 16)
  350. drv_data->ops = &bfin_sport_transfer_ops_u16;
  351. else
  352. drv_data->ops = &bfin_sport_transfer_ops_u8;
  353. bfin_write(&drv_data->regs->tcr2, bits_per_word - 1);
  354. bfin_write(&drv_data->regs->tfsdiv, bits_per_word - 1);
  355. bfin_write(&drv_data->regs->rcr2, bits_per_word - 1);
  356. drv_data->state = RUNNING_STATE;
  357. if (drv_data->cs_change)
  358. bfin_sport_spi_cs_active(chip);
  359. dev_dbg(drv_data->dev,
  360. "now pumping a transfer: width is %d, len is %d\n",
  361. bits_per_word, transfer->len);
  362. /* PIO mode write then read */
  363. dev_dbg(drv_data->dev, "doing IO transfer\n");
  364. bfin_sport_spi_enable(drv_data);
  365. if (full_duplex) {
  366. /* full duplex mode */
  367. BUG_ON((drv_data->tx_end - drv_data->tx) !=
  368. (drv_data->rx_end - drv_data->rx));
  369. drv_data->ops->duplex(drv_data);
  370. if (drv_data->tx != drv_data->tx_end)
  371. tranf_success = 0;
  372. } else if (drv_data->tx != NULL) {
  373. /* write only half duplex */
  374. drv_data->ops->write(drv_data);
  375. if (drv_data->tx != drv_data->tx_end)
  376. tranf_success = 0;
  377. } else if (drv_data->rx != NULL) {
  378. /* read only half duplex */
  379. drv_data->ops->read(drv_data);
  380. if (drv_data->rx != drv_data->rx_end)
  381. tranf_success = 0;
  382. }
  383. bfin_sport_spi_disable(drv_data);
  384. if (!tranf_success) {
  385. dev_dbg(drv_data->dev, "IO write error!\n");
  386. drv_data->state = ERROR_STATE;
  387. } else {
  388. /* Update total byte transferred */
  389. message->actual_length += transfer->len;
  390. /* Move to next transfer of this msg */
  391. drv_data->state = bfin_sport_spi_next_transfer(drv_data);
  392. if (drv_data->cs_change)
  393. bfin_sport_spi_cs_deactive(chip);
  394. }
  395. /* Schedule next transfer tasklet */
  396. tasklet_schedule(&drv_data->pump_transfers);
  397. }
  398. /* pop a msg from queue and kick off real transfer */
  399. static void
  400. bfin_sport_spi_pump_messages(struct work_struct *work)
  401. {
  402. struct bfin_sport_spi_master_data *drv_data;
  403. unsigned long flags;
  404. struct spi_message *next_msg;
  405. drv_data = container_of(work, struct bfin_sport_spi_master_data, pump_messages);
  406. /* Lock queue and check for queue work */
  407. spin_lock_irqsave(&drv_data->lock, flags);
  408. if (list_empty(&drv_data->queue) || !drv_data->run) {
  409. /* pumper kicked off but no work to do */
  410. drv_data->busy = 0;
  411. spin_unlock_irqrestore(&drv_data->lock, flags);
  412. return;
  413. }
  414. /* Make sure we are not already running a message */
  415. if (drv_data->cur_msg) {
  416. spin_unlock_irqrestore(&drv_data->lock, flags);
  417. return;
  418. }
  419. /* Extract head of queue */
  420. next_msg = list_entry(drv_data->queue.next,
  421. struct spi_message, queue);
  422. drv_data->cur_msg = next_msg;
  423. /* Setup the SSP using the per chip configuration */
  424. drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
  425. list_del_init(&drv_data->cur_msg->queue);
  426. /* Initialize message state */
  427. drv_data->cur_msg->state = START_STATE;
  428. drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
  429. struct spi_transfer, transfer_list);
  430. bfin_sport_spi_restore_state(drv_data);
  431. dev_dbg(drv_data->dev, "got a message to pump, "
  432. "state is set to: baud %d, cs_gpio %i, ctl 0x%x\n",
  433. drv_data->cur_chip->baud, drv_data->cur_chip->cs_gpio,
  434. drv_data->cur_chip->ctl_reg);
  435. dev_dbg(drv_data->dev,
  436. "the first transfer len is %d\n",
  437. drv_data->cur_transfer->len);
  438. /* Mark as busy and launch transfers */
  439. tasklet_schedule(&drv_data->pump_transfers);
  440. drv_data->busy = 1;
  441. spin_unlock_irqrestore(&drv_data->lock, flags);
  442. }
  443. /*
  444. * got a msg to transfer, queue it in drv_data->queue.
  445. * And kick off message pumper
  446. */
  447. static int
  448. bfin_sport_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  449. {
  450. struct bfin_sport_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
  451. unsigned long flags;
  452. spin_lock_irqsave(&drv_data->lock, flags);
  453. if (!drv_data->run) {
  454. spin_unlock_irqrestore(&drv_data->lock, flags);
  455. return -ESHUTDOWN;
  456. }
  457. msg->actual_length = 0;
  458. msg->status = -EINPROGRESS;
  459. msg->state = START_STATE;
  460. dev_dbg(&spi->dev, "adding an msg in transfer()\n");
  461. list_add_tail(&msg->queue, &drv_data->queue);
  462. if (drv_data->run && !drv_data->busy)
  463. queue_work(drv_data->workqueue, &drv_data->pump_messages);
  464. spin_unlock_irqrestore(&drv_data->lock, flags);
  465. return 0;
  466. }
  467. /* Called every time common spi devices change state */
  468. static int
  469. bfin_sport_spi_setup(struct spi_device *spi)
  470. {
  471. struct bfin_sport_spi_slave_data *chip, *first = NULL;
  472. int ret;
  473. /* Only alloc (or use chip_info) on first setup */
  474. chip = spi_get_ctldata(spi);
  475. if (chip == NULL) {
  476. struct bfin5xx_spi_chip *chip_info;
  477. chip = first = kzalloc(sizeof(*chip), GFP_KERNEL);
  478. if (!chip)
  479. return -ENOMEM;
  480. /* platform chip_info isn't required */
  481. chip_info = spi->controller_data;
  482. if (chip_info) {
  483. /*
  484. * DITFS and TDTYPE are only thing we don't set, but
  485. * they probably shouldn't be changed by people.
  486. */
  487. if (chip_info->ctl_reg || chip_info->enable_dma) {
  488. ret = -EINVAL;
  489. dev_err(&spi->dev, "don't set ctl_reg/enable_dma fields\n");
  490. goto error;
  491. }
  492. chip->cs_chg_udelay = chip_info->cs_chg_udelay;
  493. chip->idle_tx_val = chip_info->idle_tx_val;
  494. }
  495. }
  496. /* translate common spi framework into our register
  497. * following configure contents are same for tx and rx.
  498. */
  499. if (spi->mode & SPI_CPHA)
  500. chip->ctl_reg &= ~TCKFE;
  501. else
  502. chip->ctl_reg |= TCKFE;
  503. if (spi->mode & SPI_LSB_FIRST)
  504. chip->ctl_reg |= TLSBIT;
  505. else
  506. chip->ctl_reg &= ~TLSBIT;
  507. /* Sport in master mode */
  508. chip->ctl_reg |= ITCLK | ITFS | TFSR | LATFS | LTFS;
  509. chip->baud = bfin_sport_hz_to_spi_baud(spi->max_speed_hz);
  510. chip->cs_gpio = spi->chip_select;
  511. ret = gpio_request(chip->cs_gpio, spi->modalias);
  512. if (ret)
  513. goto error;
  514. dev_dbg(&spi->dev, "setup spi chip %s, width is %d\n",
  515. spi->modalias, spi->bits_per_word);
  516. dev_dbg(&spi->dev, "ctl_reg is 0x%x, GPIO is %i\n",
  517. chip->ctl_reg, spi->chip_select);
  518. spi_set_ctldata(spi, chip);
  519. bfin_sport_spi_cs_deactive(chip);
  520. return ret;
  521. error:
  522. kfree(first);
  523. return ret;
  524. }
  525. /*
  526. * callback for spi framework.
  527. * clean driver specific data
  528. */
  529. static void
  530. bfin_sport_spi_cleanup(struct spi_device *spi)
  531. {
  532. struct bfin_sport_spi_slave_data *chip = spi_get_ctldata(spi);
  533. if (!chip)
  534. return;
  535. gpio_free(chip->cs_gpio);
  536. kfree(chip);
  537. }
  538. static int
  539. bfin_sport_spi_init_queue(struct bfin_sport_spi_master_data *drv_data)
  540. {
  541. INIT_LIST_HEAD(&drv_data->queue);
  542. spin_lock_init(&drv_data->lock);
  543. drv_data->run = false;
  544. drv_data->busy = 0;
  545. /* init transfer tasklet */
  546. tasklet_init(&drv_data->pump_transfers,
  547. bfin_sport_spi_pump_transfers, (unsigned long)drv_data);
  548. /* init messages workqueue */
  549. INIT_WORK(&drv_data->pump_messages, bfin_sport_spi_pump_messages);
  550. drv_data->workqueue =
  551. create_singlethread_workqueue(dev_name(drv_data->master->dev.parent));
  552. if (drv_data->workqueue == NULL)
  553. return -EBUSY;
  554. return 0;
  555. }
  556. static int
  557. bfin_sport_spi_start_queue(struct bfin_sport_spi_master_data *drv_data)
  558. {
  559. unsigned long flags;
  560. spin_lock_irqsave(&drv_data->lock, flags);
  561. if (drv_data->run || drv_data->busy) {
  562. spin_unlock_irqrestore(&drv_data->lock, flags);
  563. return -EBUSY;
  564. }
  565. drv_data->run = true;
  566. drv_data->cur_msg = NULL;
  567. drv_data->cur_transfer = NULL;
  568. drv_data->cur_chip = NULL;
  569. spin_unlock_irqrestore(&drv_data->lock, flags);
  570. queue_work(drv_data->workqueue, &drv_data->pump_messages);
  571. return 0;
  572. }
  573. static inline int
  574. bfin_sport_spi_stop_queue(struct bfin_sport_spi_master_data *drv_data)
  575. {
  576. unsigned long flags;
  577. unsigned limit = 500;
  578. int status = 0;
  579. spin_lock_irqsave(&drv_data->lock, flags);
  580. /*
  581. * This is a bit lame, but is optimized for the common execution path.
  582. * A wait_queue on the drv_data->busy could be used, but then the common
  583. * execution path (pump_messages) would be required to call wake_up or
  584. * friends on every SPI message. Do this instead
  585. */
  586. drv_data->run = false;
  587. while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
  588. spin_unlock_irqrestore(&drv_data->lock, flags);
  589. msleep(10);
  590. spin_lock_irqsave(&drv_data->lock, flags);
  591. }
  592. if (!list_empty(&drv_data->queue) || drv_data->busy)
  593. status = -EBUSY;
  594. spin_unlock_irqrestore(&drv_data->lock, flags);
  595. return status;
  596. }
  597. static inline int
  598. bfin_sport_spi_destroy_queue(struct bfin_sport_spi_master_data *drv_data)
  599. {
  600. int status;
  601. status = bfin_sport_spi_stop_queue(drv_data);
  602. if (status)
  603. return status;
  604. destroy_workqueue(drv_data->workqueue);
  605. return 0;
  606. }
  607. static int bfin_sport_spi_probe(struct platform_device *pdev)
  608. {
  609. struct device *dev = &pdev->dev;
  610. struct bfin5xx_spi_master *platform_info;
  611. struct spi_master *master;
  612. struct resource *res, *ires;
  613. struct bfin_sport_spi_master_data *drv_data;
  614. int status;
  615. platform_info = dev_get_platdata(dev);
  616. /* Allocate master with space for drv_data */
  617. master = spi_alloc_master(dev, sizeof(*master) + 16);
  618. if (!master) {
  619. dev_err(dev, "cannot alloc spi_master\n");
  620. return -ENOMEM;
  621. }
  622. drv_data = spi_master_get_devdata(master);
  623. drv_data->master = master;
  624. drv_data->dev = dev;
  625. drv_data->pin_req = platform_info->pin_req;
  626. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
  627. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  628. master->bus_num = pdev->id;
  629. master->num_chipselect = platform_info->num_chipselect;
  630. master->cleanup = bfin_sport_spi_cleanup;
  631. master->setup = bfin_sport_spi_setup;
  632. master->transfer = bfin_sport_spi_transfer;
  633. /* Find and map our resources */
  634. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  635. if (res == NULL) {
  636. dev_err(dev, "cannot get IORESOURCE_MEM\n");
  637. status = -ENOENT;
  638. goto out_error_get_res;
  639. }
  640. drv_data->regs = ioremap(res->start, resource_size(res));
  641. if (drv_data->regs == NULL) {
  642. dev_err(dev, "cannot map registers\n");
  643. status = -ENXIO;
  644. goto out_error_ioremap;
  645. }
  646. ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  647. if (!ires) {
  648. dev_err(dev, "cannot get IORESOURCE_IRQ\n");
  649. status = -ENODEV;
  650. goto out_error_get_ires;
  651. }
  652. drv_data->err_irq = ires->start;
  653. /* Initial and start queue */
  654. status = bfin_sport_spi_init_queue(drv_data);
  655. if (status) {
  656. dev_err(dev, "problem initializing queue\n");
  657. goto out_error_queue_alloc;
  658. }
  659. status = bfin_sport_spi_start_queue(drv_data);
  660. if (status) {
  661. dev_err(dev, "problem starting queue\n");
  662. goto out_error_queue_alloc;
  663. }
  664. status = request_irq(drv_data->err_irq, sport_err_handler,
  665. 0, "sport_spi_err", drv_data);
  666. if (status) {
  667. dev_err(dev, "unable to request sport err irq\n");
  668. goto out_error_irq;
  669. }
  670. status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
  671. if (status) {
  672. dev_err(dev, "requesting peripherals failed\n");
  673. goto out_error_peripheral;
  674. }
  675. /* Register with the SPI framework */
  676. platform_set_drvdata(pdev, drv_data);
  677. status = spi_register_master(master);
  678. if (status) {
  679. dev_err(dev, "problem registering spi master\n");
  680. goto out_error_master;
  681. }
  682. dev_info(dev, "%s, regs_base@%p\n", DRV_DESC, drv_data->regs);
  683. return 0;
  684. out_error_master:
  685. peripheral_free_list(drv_data->pin_req);
  686. out_error_peripheral:
  687. free_irq(drv_data->err_irq, drv_data);
  688. out_error_irq:
  689. out_error_queue_alloc:
  690. bfin_sport_spi_destroy_queue(drv_data);
  691. out_error_get_ires:
  692. iounmap(drv_data->regs);
  693. out_error_ioremap:
  694. out_error_get_res:
  695. spi_master_put(master);
  696. return status;
  697. }
  698. /* stop hardware and remove the driver */
  699. static int bfin_sport_spi_remove(struct platform_device *pdev)
  700. {
  701. struct bfin_sport_spi_master_data *drv_data = platform_get_drvdata(pdev);
  702. int status = 0;
  703. if (!drv_data)
  704. return 0;
  705. /* Remove the queue */
  706. status = bfin_sport_spi_destroy_queue(drv_data);
  707. if (status)
  708. return status;
  709. /* Disable the SSP at the peripheral and SOC level */
  710. bfin_sport_spi_disable(drv_data);
  711. /* Disconnect from the SPI framework */
  712. spi_unregister_master(drv_data->master);
  713. peripheral_free_list(drv_data->pin_req);
  714. return 0;
  715. }
  716. #ifdef CONFIG_PM_SLEEP
  717. static int bfin_sport_spi_suspend(struct device *dev)
  718. {
  719. struct bfin_sport_spi_master_data *drv_data = dev_get_drvdata(dev);
  720. int status;
  721. status = bfin_sport_spi_stop_queue(drv_data);
  722. if (status)
  723. return status;
  724. /* stop hardware */
  725. bfin_sport_spi_disable(drv_data);
  726. return status;
  727. }
  728. static int bfin_sport_spi_resume(struct device *dev)
  729. {
  730. struct bfin_sport_spi_master_data *drv_data = dev_get_drvdata(dev);
  731. int status;
  732. /* Enable the SPI interface */
  733. bfin_sport_spi_enable(drv_data);
  734. /* Start the queue running */
  735. status = bfin_sport_spi_start_queue(drv_data);
  736. if (status)
  737. dev_err(drv_data->dev, "problem resuming queue\n");
  738. return status;
  739. }
  740. static SIMPLE_DEV_PM_OPS(bfin_sport_spi_pm_ops, bfin_sport_spi_suspend,
  741. bfin_sport_spi_resume);
  742. #define BFIN_SPORT_SPI_PM_OPS (&bfin_sport_spi_pm_ops)
  743. #else
  744. #define BFIN_SPORT_SPI_PM_OPS NULL
  745. #endif
  746. static struct platform_driver bfin_sport_spi_driver = {
  747. .driver = {
  748. .name = DRV_NAME,
  749. .pm = BFIN_SPORT_SPI_PM_OPS,
  750. },
  751. .probe = bfin_sport_spi_probe,
  752. .remove = bfin_sport_spi_remove,
  753. };
  754. module_platform_driver(bfin_sport_spi_driver);