spi-bfin5xx.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. /*
  2. * Blackfin On-Chip SPI Driver
  3. *
  4. * Copyright 2004-2010 Analog Devices Inc.
  5. *
  6. * Enter bugs at http://blackfin.uclinux.org/
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/gpio.h>
  15. #include <linux/slab.h>
  16. #include <linux/io.h>
  17. #include <linux/ioport.h>
  18. #include <linux/irq.h>
  19. #include <linux/errno.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/workqueue.h>
  25. #include <asm/dma.h>
  26. #include <asm/portmux.h>
  27. #include <asm/bfin5xx_spi.h>
  28. #include <asm/cacheflush.h>
  29. #define DRV_NAME "bfin-spi"
  30. #define DRV_AUTHOR "Bryan Wu, Luke Yang"
  31. #define DRV_DESC "Blackfin on-chip SPI Controller Driver"
  32. #define DRV_VERSION "1.0"
  33. MODULE_AUTHOR(DRV_AUTHOR);
  34. MODULE_DESCRIPTION(DRV_DESC);
  35. MODULE_LICENSE("GPL");
  36. #define START_STATE ((void *)0)
  37. #define RUNNING_STATE ((void *)1)
  38. #define DONE_STATE ((void *)2)
  39. #define ERROR_STATE ((void *)-1)
  40. struct bfin_spi_master_data;
  41. struct bfin_spi_transfer_ops {
  42. void (*write) (struct bfin_spi_master_data *);
  43. void (*read) (struct bfin_spi_master_data *);
  44. void (*duplex) (struct bfin_spi_master_data *);
  45. };
  46. struct bfin_spi_master_data {
  47. /* Driver model hookup */
  48. struct platform_device *pdev;
  49. /* SPI framework hookup */
  50. struct spi_master *master;
  51. /* Regs base of SPI controller */
  52. struct bfin_spi_regs __iomem *regs;
  53. /* Pin request list */
  54. u16 *pin_req;
  55. /* BFIN hookup */
  56. struct bfin5xx_spi_master *master_info;
  57. /* Driver message queue */
  58. struct workqueue_struct *workqueue;
  59. struct work_struct pump_messages;
  60. spinlock_t lock;
  61. struct list_head queue;
  62. int busy;
  63. bool running;
  64. /* Message Transfer pump */
  65. struct tasklet_struct pump_transfers;
  66. /* Current message transfer state info */
  67. struct spi_message *cur_msg;
  68. struct spi_transfer *cur_transfer;
  69. struct bfin_spi_slave_data *cur_chip;
  70. size_t len_in_bytes;
  71. size_t len;
  72. void *tx;
  73. void *tx_end;
  74. void *rx;
  75. void *rx_end;
  76. /* DMA stuffs */
  77. int dma_channel;
  78. int dma_mapped;
  79. int dma_requested;
  80. dma_addr_t rx_dma;
  81. dma_addr_t tx_dma;
  82. int irq_requested;
  83. int spi_irq;
  84. size_t rx_map_len;
  85. size_t tx_map_len;
  86. u8 n_bytes;
  87. u16 ctrl_reg;
  88. u16 flag_reg;
  89. int cs_change;
  90. const struct bfin_spi_transfer_ops *ops;
  91. };
  92. struct bfin_spi_slave_data {
  93. u16 ctl_reg;
  94. u16 baud;
  95. u16 flag;
  96. u8 chip_select_num;
  97. u8 enable_dma;
  98. u16 cs_chg_udelay; /* Some devices require > 255usec delay */
  99. u32 cs_gpio;
  100. u16 idle_tx_val;
  101. u8 pio_interrupt; /* use spi data irq */
  102. const struct bfin_spi_transfer_ops *ops;
  103. };
  104. static void bfin_spi_enable(struct bfin_spi_master_data *drv_data)
  105. {
  106. bfin_write_or(&drv_data->regs->ctl, BIT_CTL_ENABLE);
  107. }
  108. static void bfin_spi_disable(struct bfin_spi_master_data *drv_data)
  109. {
  110. bfin_write_and(&drv_data->regs->ctl, ~BIT_CTL_ENABLE);
  111. }
  112. /* Caculate the SPI_BAUD register value based on input HZ */
  113. static u16 hz_to_spi_baud(u32 speed_hz)
  114. {
  115. u_long sclk = get_sclk();
  116. u16 spi_baud = (sclk / (2 * speed_hz));
  117. if ((sclk % (2 * speed_hz)) > 0)
  118. spi_baud++;
  119. if (spi_baud < MIN_SPI_BAUD_VAL)
  120. spi_baud = MIN_SPI_BAUD_VAL;
  121. return spi_baud;
  122. }
  123. static int bfin_spi_flush(struct bfin_spi_master_data *drv_data)
  124. {
  125. unsigned long limit = loops_per_jiffy << 1;
  126. /* wait for stop and clear stat */
  127. while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF) && --limit)
  128. cpu_relax();
  129. bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
  130. return limit;
  131. }
  132. /* Chip select operation functions for cs_change flag */
  133. static void bfin_spi_cs_active(struct bfin_spi_master_data *drv_data, struct bfin_spi_slave_data *chip)
  134. {
  135. if (likely(chip->chip_select_num < MAX_CTRL_CS))
  136. bfin_write_and(&drv_data->regs->flg, ~chip->flag);
  137. else
  138. gpio_set_value(chip->cs_gpio, 0);
  139. }
  140. static void bfin_spi_cs_deactive(struct bfin_spi_master_data *drv_data,
  141. struct bfin_spi_slave_data *chip)
  142. {
  143. if (likely(chip->chip_select_num < MAX_CTRL_CS))
  144. bfin_write_or(&drv_data->regs->flg, chip->flag);
  145. else
  146. gpio_set_value(chip->cs_gpio, 1);
  147. /* Move delay here for consistency */
  148. if (chip->cs_chg_udelay)
  149. udelay(chip->cs_chg_udelay);
  150. }
  151. /* enable or disable the pin muxed by GPIO and SPI CS to work as SPI CS */
  152. static inline void bfin_spi_cs_enable(struct bfin_spi_master_data *drv_data,
  153. struct bfin_spi_slave_data *chip)
  154. {
  155. if (chip->chip_select_num < MAX_CTRL_CS)
  156. bfin_write_or(&drv_data->regs->flg, chip->flag >> 8);
  157. }
  158. static inline void bfin_spi_cs_disable(struct bfin_spi_master_data *drv_data,
  159. struct bfin_spi_slave_data *chip)
  160. {
  161. if (chip->chip_select_num < MAX_CTRL_CS)
  162. bfin_write_and(&drv_data->regs->flg, ~(chip->flag >> 8));
  163. }
  164. /* stop controller and re-config current chip*/
  165. static void bfin_spi_restore_state(struct bfin_spi_master_data *drv_data)
  166. {
  167. struct bfin_spi_slave_data *chip = drv_data->cur_chip;
  168. /* Clear status and disable clock */
  169. bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
  170. bfin_spi_disable(drv_data);
  171. dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
  172. SSYNC();
  173. /* Load the registers */
  174. bfin_write(&drv_data->regs->ctl, chip->ctl_reg);
  175. bfin_write(&drv_data->regs->baud, chip->baud);
  176. bfin_spi_enable(drv_data);
  177. bfin_spi_cs_active(drv_data, chip);
  178. }
  179. /* used to kick off transfer in rx mode and read unwanted RX data */
  180. static inline void bfin_spi_dummy_read(struct bfin_spi_master_data *drv_data)
  181. {
  182. (void) bfin_read(&drv_data->regs->rdbr);
  183. }
  184. static void bfin_spi_u8_writer(struct bfin_spi_master_data *drv_data)
  185. {
  186. /* clear RXS (we check for RXS inside the loop) */
  187. bfin_spi_dummy_read(drv_data);
  188. while (drv_data->tx < drv_data->tx_end) {
  189. bfin_write(&drv_data->regs->tdbr, (*(u8 *) (drv_data->tx++)));
  190. /* wait until transfer finished.
  191. checking SPIF or TXS may not guarantee transfer completion */
  192. while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
  193. cpu_relax();
  194. /* discard RX data and clear RXS */
  195. bfin_spi_dummy_read(drv_data);
  196. }
  197. }
  198. static void bfin_spi_u8_reader(struct bfin_spi_master_data *drv_data)
  199. {
  200. u16 tx_val = drv_data->cur_chip->idle_tx_val;
  201. /* discard old RX data and clear RXS */
  202. bfin_spi_dummy_read(drv_data);
  203. while (drv_data->rx < drv_data->rx_end) {
  204. bfin_write(&drv_data->regs->tdbr, tx_val);
  205. while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
  206. cpu_relax();
  207. *(u8 *) (drv_data->rx++) = bfin_read(&drv_data->regs->rdbr);
  208. }
  209. }
  210. static void bfin_spi_u8_duplex(struct bfin_spi_master_data *drv_data)
  211. {
  212. /* discard old RX data and clear RXS */
  213. bfin_spi_dummy_read(drv_data);
  214. while (drv_data->rx < drv_data->rx_end) {
  215. bfin_write(&drv_data->regs->tdbr, (*(u8 *) (drv_data->tx++)));
  216. while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
  217. cpu_relax();
  218. *(u8 *) (drv_data->rx++) = bfin_read(&drv_data->regs->rdbr);
  219. }
  220. }
  221. static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u8 = {
  222. .write = bfin_spi_u8_writer,
  223. .read = bfin_spi_u8_reader,
  224. .duplex = bfin_spi_u8_duplex,
  225. };
  226. static void bfin_spi_u16_writer(struct bfin_spi_master_data *drv_data)
  227. {
  228. /* clear RXS (we check for RXS inside the loop) */
  229. bfin_spi_dummy_read(drv_data);
  230. while (drv_data->tx < drv_data->tx_end) {
  231. bfin_write(&drv_data->regs->tdbr, (*(u16 *) (drv_data->tx)));
  232. drv_data->tx += 2;
  233. /* wait until transfer finished.
  234. checking SPIF or TXS may not guarantee transfer completion */
  235. while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
  236. cpu_relax();
  237. /* discard RX data and clear RXS */
  238. bfin_spi_dummy_read(drv_data);
  239. }
  240. }
  241. static void bfin_spi_u16_reader(struct bfin_spi_master_data *drv_data)
  242. {
  243. u16 tx_val = drv_data->cur_chip->idle_tx_val;
  244. /* discard old RX data and clear RXS */
  245. bfin_spi_dummy_read(drv_data);
  246. while (drv_data->rx < drv_data->rx_end) {
  247. bfin_write(&drv_data->regs->tdbr, tx_val);
  248. while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
  249. cpu_relax();
  250. *(u16 *) (drv_data->rx) = bfin_read(&drv_data->regs->rdbr);
  251. drv_data->rx += 2;
  252. }
  253. }
  254. static void bfin_spi_u16_duplex(struct bfin_spi_master_data *drv_data)
  255. {
  256. /* discard old RX data and clear RXS */
  257. bfin_spi_dummy_read(drv_data);
  258. while (drv_data->rx < drv_data->rx_end) {
  259. bfin_write(&drv_data->regs->tdbr, (*(u16 *) (drv_data->tx)));
  260. drv_data->tx += 2;
  261. while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
  262. cpu_relax();
  263. *(u16 *) (drv_data->rx) = bfin_read(&drv_data->regs->rdbr);
  264. drv_data->rx += 2;
  265. }
  266. }
  267. static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u16 = {
  268. .write = bfin_spi_u16_writer,
  269. .read = bfin_spi_u16_reader,
  270. .duplex = bfin_spi_u16_duplex,
  271. };
  272. /* test if there is more transfer to be done */
  273. static void *bfin_spi_next_transfer(struct bfin_spi_master_data *drv_data)
  274. {
  275. struct spi_message *msg = drv_data->cur_msg;
  276. struct spi_transfer *trans = drv_data->cur_transfer;
  277. /* Move to next transfer */
  278. if (trans->transfer_list.next != &msg->transfers) {
  279. drv_data->cur_transfer =
  280. list_entry(trans->transfer_list.next,
  281. struct spi_transfer, transfer_list);
  282. return RUNNING_STATE;
  283. } else
  284. return DONE_STATE;
  285. }
  286. /*
  287. * caller already set message->status;
  288. * dma and pio irqs are blocked give finished message back
  289. */
  290. static void bfin_spi_giveback(struct bfin_spi_master_data *drv_data)
  291. {
  292. struct bfin_spi_slave_data *chip = drv_data->cur_chip;
  293. unsigned long flags;
  294. struct spi_message *msg;
  295. spin_lock_irqsave(&drv_data->lock, flags);
  296. msg = drv_data->cur_msg;
  297. drv_data->cur_msg = NULL;
  298. drv_data->cur_transfer = NULL;
  299. drv_data->cur_chip = NULL;
  300. queue_work(drv_data->workqueue, &drv_data->pump_messages);
  301. spin_unlock_irqrestore(&drv_data->lock, flags);
  302. msg->state = NULL;
  303. if (!drv_data->cs_change)
  304. bfin_spi_cs_deactive(drv_data, chip);
  305. /* Not stop spi in autobuffer mode */
  306. if (drv_data->tx_dma != 0xFFFF)
  307. bfin_spi_disable(drv_data);
  308. if (msg->complete)
  309. msg->complete(msg->context);
  310. }
  311. /* spi data irq handler */
  312. static irqreturn_t bfin_spi_pio_irq_handler(int irq, void *dev_id)
  313. {
  314. struct bfin_spi_master_data *drv_data = dev_id;
  315. struct bfin_spi_slave_data *chip = drv_data->cur_chip;
  316. struct spi_message *msg = drv_data->cur_msg;
  317. int n_bytes = drv_data->n_bytes;
  318. int loop = 0;
  319. /* wait until transfer finished. */
  320. while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
  321. cpu_relax();
  322. if ((drv_data->tx && drv_data->tx >= drv_data->tx_end) ||
  323. (drv_data->rx && drv_data->rx >= (drv_data->rx_end - n_bytes))) {
  324. /* last read */
  325. if (drv_data->rx) {
  326. dev_dbg(&drv_data->pdev->dev, "last read\n");
  327. if (!(n_bytes % 2)) {
  328. u16 *buf = (u16 *)drv_data->rx;
  329. for (loop = 0; loop < n_bytes / 2; loop++)
  330. *buf++ = bfin_read(&drv_data->regs->rdbr);
  331. } else {
  332. u8 *buf = (u8 *)drv_data->rx;
  333. for (loop = 0; loop < n_bytes; loop++)
  334. *buf++ = bfin_read(&drv_data->regs->rdbr);
  335. }
  336. drv_data->rx += n_bytes;
  337. }
  338. msg->actual_length += drv_data->len_in_bytes;
  339. if (drv_data->cs_change)
  340. bfin_spi_cs_deactive(drv_data, chip);
  341. /* Move to next transfer */
  342. msg->state = bfin_spi_next_transfer(drv_data);
  343. disable_irq_nosync(drv_data->spi_irq);
  344. /* Schedule transfer tasklet */
  345. tasklet_schedule(&drv_data->pump_transfers);
  346. return IRQ_HANDLED;
  347. }
  348. if (drv_data->rx && drv_data->tx) {
  349. /* duplex */
  350. dev_dbg(&drv_data->pdev->dev, "duplex: write_TDBR\n");
  351. if (!(n_bytes % 2)) {
  352. u16 *buf = (u16 *)drv_data->rx;
  353. u16 *buf2 = (u16 *)drv_data->tx;
  354. for (loop = 0; loop < n_bytes / 2; loop++) {
  355. *buf++ = bfin_read(&drv_data->regs->rdbr);
  356. bfin_write(&drv_data->regs->tdbr, *buf2++);
  357. }
  358. } else {
  359. u8 *buf = (u8 *)drv_data->rx;
  360. u8 *buf2 = (u8 *)drv_data->tx;
  361. for (loop = 0; loop < n_bytes; loop++) {
  362. *buf++ = bfin_read(&drv_data->regs->rdbr);
  363. bfin_write(&drv_data->regs->tdbr, *buf2++);
  364. }
  365. }
  366. } else if (drv_data->rx) {
  367. /* read */
  368. dev_dbg(&drv_data->pdev->dev, "read: write_TDBR\n");
  369. if (!(n_bytes % 2)) {
  370. u16 *buf = (u16 *)drv_data->rx;
  371. for (loop = 0; loop < n_bytes / 2; loop++) {
  372. *buf++ = bfin_read(&drv_data->regs->rdbr);
  373. bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
  374. }
  375. } else {
  376. u8 *buf = (u8 *)drv_data->rx;
  377. for (loop = 0; loop < n_bytes; loop++) {
  378. *buf++ = bfin_read(&drv_data->regs->rdbr);
  379. bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
  380. }
  381. }
  382. } else if (drv_data->tx) {
  383. /* write */
  384. dev_dbg(&drv_data->pdev->dev, "write: write_TDBR\n");
  385. if (!(n_bytes % 2)) {
  386. u16 *buf = (u16 *)drv_data->tx;
  387. for (loop = 0; loop < n_bytes / 2; loop++) {
  388. bfin_read(&drv_data->regs->rdbr);
  389. bfin_write(&drv_data->regs->tdbr, *buf++);
  390. }
  391. } else {
  392. u8 *buf = (u8 *)drv_data->tx;
  393. for (loop = 0; loop < n_bytes; loop++) {
  394. bfin_read(&drv_data->regs->rdbr);
  395. bfin_write(&drv_data->regs->tdbr, *buf++);
  396. }
  397. }
  398. }
  399. if (drv_data->tx)
  400. drv_data->tx += n_bytes;
  401. if (drv_data->rx)
  402. drv_data->rx += n_bytes;
  403. return IRQ_HANDLED;
  404. }
  405. static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
  406. {
  407. struct bfin_spi_master_data *drv_data = dev_id;
  408. struct bfin_spi_slave_data *chip = drv_data->cur_chip;
  409. struct spi_message *msg = drv_data->cur_msg;
  410. unsigned long timeout;
  411. unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel);
  412. u16 spistat = bfin_read(&drv_data->regs->stat);
  413. dev_dbg(&drv_data->pdev->dev,
  414. "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
  415. dmastat, spistat);
  416. if (drv_data->rx != NULL) {
  417. u16 cr = bfin_read(&drv_data->regs->ctl);
  418. /* discard old RX data and clear RXS */
  419. bfin_spi_dummy_read(drv_data);
  420. bfin_write(&drv_data->regs->ctl, cr & ~BIT_CTL_ENABLE); /* Disable SPI */
  421. bfin_write(&drv_data->regs->ctl, cr & ~BIT_CTL_TIMOD); /* Restore State */
  422. bfin_write(&drv_data->regs->stat, BIT_STAT_CLR); /* Clear Status */
  423. }
  424. clear_dma_irqstat(drv_data->dma_channel);
  425. /*
  426. * wait for the last transaction shifted out. HRM states:
  427. * at this point there may still be data in the SPI DMA FIFO waiting
  428. * to be transmitted ... software needs to poll TXS in the SPI_STAT
  429. * register until it goes low for 2 successive reads
  430. */
  431. if (drv_data->tx != NULL) {
  432. while ((bfin_read(&drv_data->regs->stat) & BIT_STAT_TXS) ||
  433. (bfin_read(&drv_data->regs->stat) & BIT_STAT_TXS))
  434. cpu_relax();
  435. }
  436. dev_dbg(&drv_data->pdev->dev,
  437. "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
  438. dmastat, bfin_read(&drv_data->regs->stat));
  439. timeout = jiffies + HZ;
  440. while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF))
  441. if (!time_before(jiffies, timeout)) {
  442. dev_warn(&drv_data->pdev->dev, "timeout waiting for SPIF\n");
  443. break;
  444. } else
  445. cpu_relax();
  446. if ((dmastat & DMA_ERR) && (spistat & BIT_STAT_RBSY)) {
  447. msg->state = ERROR_STATE;
  448. dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n");
  449. } else {
  450. msg->actual_length += drv_data->len_in_bytes;
  451. if (drv_data->cs_change)
  452. bfin_spi_cs_deactive(drv_data, chip);
  453. /* Move to next transfer */
  454. msg->state = bfin_spi_next_transfer(drv_data);
  455. }
  456. /* Schedule transfer tasklet */
  457. tasklet_schedule(&drv_data->pump_transfers);
  458. /* free the irq handler before next transfer */
  459. dev_dbg(&drv_data->pdev->dev,
  460. "disable dma channel irq%d\n",
  461. drv_data->dma_channel);
  462. dma_disable_irq_nosync(drv_data->dma_channel);
  463. return IRQ_HANDLED;
  464. }
  465. static void bfin_spi_pump_transfers(unsigned long data)
  466. {
  467. struct bfin_spi_master_data *drv_data = (struct bfin_spi_master_data *)data;
  468. struct spi_message *message = NULL;
  469. struct spi_transfer *transfer = NULL;
  470. struct spi_transfer *previous = NULL;
  471. struct bfin_spi_slave_data *chip = NULL;
  472. unsigned int bits_per_word;
  473. u16 cr, cr_width = 0, dma_width, dma_config;
  474. u32 tranf_success = 1;
  475. u8 full_duplex = 0;
  476. /* Get current state information */
  477. message = drv_data->cur_msg;
  478. transfer = drv_data->cur_transfer;
  479. chip = drv_data->cur_chip;
  480. /*
  481. * if msg is error or done, report it back using complete() callback
  482. */
  483. /* Handle for abort */
  484. if (message->state == ERROR_STATE) {
  485. dev_dbg(&drv_data->pdev->dev, "transfer: we've hit an error\n");
  486. message->status = -EIO;
  487. bfin_spi_giveback(drv_data);
  488. return;
  489. }
  490. /* Handle end of message */
  491. if (message->state == DONE_STATE) {
  492. dev_dbg(&drv_data->pdev->dev, "transfer: all done!\n");
  493. message->status = 0;
  494. bfin_spi_flush(drv_data);
  495. bfin_spi_giveback(drv_data);
  496. return;
  497. }
  498. /* Delay if requested at end of transfer */
  499. if (message->state == RUNNING_STATE) {
  500. dev_dbg(&drv_data->pdev->dev, "transfer: still running ...\n");
  501. previous = list_entry(transfer->transfer_list.prev,
  502. struct spi_transfer, transfer_list);
  503. if (previous->delay_usecs)
  504. udelay(previous->delay_usecs);
  505. }
  506. /* Flush any existing transfers that may be sitting in the hardware */
  507. if (bfin_spi_flush(drv_data) == 0) {
  508. dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
  509. message->status = -EIO;
  510. bfin_spi_giveback(drv_data);
  511. return;
  512. }
  513. if (transfer->len == 0) {
  514. /* Move to next transfer of this msg */
  515. message->state = bfin_spi_next_transfer(drv_data);
  516. /* Schedule next transfer tasklet */
  517. tasklet_schedule(&drv_data->pump_transfers);
  518. return;
  519. }
  520. if (transfer->tx_buf != NULL) {
  521. drv_data->tx = (void *)transfer->tx_buf;
  522. drv_data->tx_end = drv_data->tx + transfer->len;
  523. dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
  524. transfer->tx_buf, drv_data->tx_end);
  525. } else {
  526. drv_data->tx = NULL;
  527. }
  528. if (transfer->rx_buf != NULL) {
  529. full_duplex = transfer->tx_buf != NULL;
  530. drv_data->rx = transfer->rx_buf;
  531. drv_data->rx_end = drv_data->rx + transfer->len;
  532. dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
  533. transfer->rx_buf, drv_data->rx_end);
  534. } else {
  535. drv_data->rx = NULL;
  536. }
  537. drv_data->rx_dma = transfer->rx_dma;
  538. drv_data->tx_dma = transfer->tx_dma;
  539. drv_data->len_in_bytes = transfer->len;
  540. drv_data->cs_change = transfer->cs_change;
  541. /* Bits per word setup */
  542. bits_per_word = transfer->bits_per_word;
  543. if (bits_per_word == 16) {
  544. drv_data->n_bytes = bits_per_word/8;
  545. drv_data->len = (transfer->len) >> 1;
  546. cr_width = BIT_CTL_WORDSIZE;
  547. drv_data->ops = &bfin_bfin_spi_transfer_ops_u16;
  548. } else if (bits_per_word == 8) {
  549. drv_data->n_bytes = bits_per_word/8;
  550. drv_data->len = transfer->len;
  551. drv_data->ops = &bfin_bfin_spi_transfer_ops_u8;
  552. }
  553. cr = bfin_read(&drv_data->regs->ctl) & ~(BIT_CTL_TIMOD | BIT_CTL_WORDSIZE);
  554. cr |= cr_width;
  555. bfin_write(&drv_data->regs->ctl, cr);
  556. dev_dbg(&drv_data->pdev->dev,
  557. "transfer: drv_data->ops is %p, chip->ops is %p, u8_ops is %p\n",
  558. drv_data->ops, chip->ops, &bfin_bfin_spi_transfer_ops_u8);
  559. message->state = RUNNING_STATE;
  560. dma_config = 0;
  561. /* Speed setup (surely valid because already checked) */
  562. if (transfer->speed_hz)
  563. bfin_write(&drv_data->regs->baud, hz_to_spi_baud(transfer->speed_hz));
  564. else
  565. bfin_write(&drv_data->regs->baud, chip->baud);
  566. bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
  567. bfin_spi_cs_active(drv_data, chip);
  568. dev_dbg(&drv_data->pdev->dev,
  569. "now pumping a transfer: width is %d, len is %d\n",
  570. cr_width, transfer->len);
  571. /*
  572. * Try to map dma buffer and do a dma transfer. If successful use,
  573. * different way to r/w according to the enable_dma settings and if
  574. * we are not doing a full duplex transfer (since the hardware does
  575. * not support full duplex DMA transfers).
  576. */
  577. if (!full_duplex && drv_data->cur_chip->enable_dma
  578. && drv_data->len > 6) {
  579. unsigned long dma_start_addr, flags;
  580. disable_dma(drv_data->dma_channel);
  581. clear_dma_irqstat(drv_data->dma_channel);
  582. /* config dma channel */
  583. dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
  584. set_dma_x_count(drv_data->dma_channel, drv_data->len);
  585. if (cr_width == BIT_CTL_WORDSIZE) {
  586. set_dma_x_modify(drv_data->dma_channel, 2);
  587. dma_width = WDSIZE_16;
  588. } else {
  589. set_dma_x_modify(drv_data->dma_channel, 1);
  590. dma_width = WDSIZE_8;
  591. }
  592. /* poll for SPI completion before start */
  593. while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF))
  594. cpu_relax();
  595. /* dirty hack for autobuffer DMA mode */
  596. if (drv_data->tx_dma == 0xFFFF) {
  597. dev_dbg(&drv_data->pdev->dev,
  598. "doing autobuffer DMA out.\n");
  599. /* no irq in autobuffer mode */
  600. dma_config =
  601. (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
  602. set_dma_config(drv_data->dma_channel, dma_config);
  603. set_dma_start_addr(drv_data->dma_channel,
  604. (unsigned long)drv_data->tx);
  605. enable_dma(drv_data->dma_channel);
  606. /* start SPI transfer */
  607. bfin_write(&drv_data->regs->ctl, cr | BIT_CTL_TIMOD_DMA_TX);
  608. /* just return here, there can only be one transfer
  609. * in this mode
  610. */
  611. message->status = 0;
  612. bfin_spi_giveback(drv_data);
  613. return;
  614. }
  615. /* In dma mode, rx or tx must be NULL in one transfer */
  616. dma_config = (RESTART | dma_width | DI_EN);
  617. if (drv_data->rx != NULL) {
  618. /* set transfer mode, and enable SPI */
  619. dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n",
  620. drv_data->rx, drv_data->len_in_bytes);
  621. /* invalidate caches, if needed */
  622. if (bfin_addr_dcacheable((unsigned long) drv_data->rx))
  623. invalidate_dcache_range((unsigned long) drv_data->rx,
  624. (unsigned long) (drv_data->rx +
  625. drv_data->len_in_bytes));
  626. dma_config |= WNR;
  627. dma_start_addr = (unsigned long)drv_data->rx;
  628. cr |= BIT_CTL_TIMOD_DMA_RX | BIT_CTL_SENDOPT;
  629. } else if (drv_data->tx != NULL) {
  630. dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
  631. /* flush caches, if needed */
  632. if (bfin_addr_dcacheable((unsigned long) drv_data->tx))
  633. flush_dcache_range((unsigned long) drv_data->tx,
  634. (unsigned long) (drv_data->tx +
  635. drv_data->len_in_bytes));
  636. dma_start_addr = (unsigned long)drv_data->tx;
  637. cr |= BIT_CTL_TIMOD_DMA_TX;
  638. } else
  639. BUG();
  640. /* oh man, here there be monsters ... and i dont mean the
  641. * fluffy cute ones from pixar, i mean the kind that'll eat
  642. * your data, kick your dog, and love it all. do *not* try
  643. * and change these lines unless you (1) heavily test DMA
  644. * with SPI flashes on a loaded system (e.g. ping floods),
  645. * (2) know just how broken the DMA engine interaction with
  646. * the SPI peripheral is, and (3) have someone else to blame
  647. * when you screw it all up anyways.
  648. */
  649. set_dma_start_addr(drv_data->dma_channel, dma_start_addr);
  650. set_dma_config(drv_data->dma_channel, dma_config);
  651. local_irq_save(flags);
  652. SSYNC();
  653. bfin_write(&drv_data->regs->ctl, cr);
  654. enable_dma(drv_data->dma_channel);
  655. dma_enable_irq(drv_data->dma_channel);
  656. local_irq_restore(flags);
  657. return;
  658. }
  659. /*
  660. * We always use SPI_WRITE mode (transfer starts with TDBR write).
  661. * SPI_READ mode (transfer starts with RDBR read) seems to have
  662. * problems with setting up the output value in TDBR prior to the
  663. * start of the transfer.
  664. */
  665. bfin_write(&drv_data->regs->ctl, cr | BIT_CTL_TXMOD);
  666. if (chip->pio_interrupt) {
  667. /* SPI irq should have been disabled by now */
  668. /* discard old RX data and clear RXS */
  669. bfin_spi_dummy_read(drv_data);
  670. /* start transfer */
  671. if (drv_data->tx == NULL)
  672. bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
  673. else {
  674. int loop;
  675. if (bits_per_word == 16) {
  676. u16 *buf = (u16 *)drv_data->tx;
  677. for (loop = 0; loop < bits_per_word / 16;
  678. loop++) {
  679. bfin_write(&drv_data->regs->tdbr, *buf++);
  680. }
  681. } else if (bits_per_word == 8) {
  682. u8 *buf = (u8 *)drv_data->tx;
  683. for (loop = 0; loop < bits_per_word / 8; loop++)
  684. bfin_write(&drv_data->regs->tdbr, *buf++);
  685. }
  686. drv_data->tx += drv_data->n_bytes;
  687. }
  688. /* once TDBR is empty, interrupt is triggered */
  689. enable_irq(drv_data->spi_irq);
  690. return;
  691. }
  692. /* IO mode */
  693. dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
  694. if (full_duplex) {
  695. /* full duplex mode */
  696. BUG_ON((drv_data->tx_end - drv_data->tx) !=
  697. (drv_data->rx_end - drv_data->rx));
  698. dev_dbg(&drv_data->pdev->dev,
  699. "IO duplex: cr is 0x%x\n", cr);
  700. drv_data->ops->duplex(drv_data);
  701. if (drv_data->tx != drv_data->tx_end)
  702. tranf_success = 0;
  703. } else if (drv_data->tx != NULL) {
  704. /* write only half duplex */
  705. dev_dbg(&drv_data->pdev->dev,
  706. "IO write: cr is 0x%x\n", cr);
  707. drv_data->ops->write(drv_data);
  708. if (drv_data->tx != drv_data->tx_end)
  709. tranf_success = 0;
  710. } else if (drv_data->rx != NULL) {
  711. /* read only half duplex */
  712. dev_dbg(&drv_data->pdev->dev,
  713. "IO read: cr is 0x%x\n", cr);
  714. drv_data->ops->read(drv_data);
  715. if (drv_data->rx != drv_data->rx_end)
  716. tranf_success = 0;
  717. }
  718. if (!tranf_success) {
  719. dev_dbg(&drv_data->pdev->dev,
  720. "IO write error!\n");
  721. message->state = ERROR_STATE;
  722. } else {
  723. /* Update total byte transferred */
  724. message->actual_length += drv_data->len_in_bytes;
  725. /* Move to next transfer of this msg */
  726. message->state = bfin_spi_next_transfer(drv_data);
  727. if (drv_data->cs_change && message->state != DONE_STATE) {
  728. bfin_spi_flush(drv_data);
  729. bfin_spi_cs_deactive(drv_data, chip);
  730. }
  731. }
  732. /* Schedule next transfer tasklet */
  733. tasklet_schedule(&drv_data->pump_transfers);
  734. }
  735. /* pop a msg from queue and kick off real transfer */
  736. static void bfin_spi_pump_messages(struct work_struct *work)
  737. {
  738. struct bfin_spi_master_data *drv_data;
  739. unsigned long flags;
  740. drv_data = container_of(work, struct bfin_spi_master_data, pump_messages);
  741. /* Lock queue and check for queue work */
  742. spin_lock_irqsave(&drv_data->lock, flags);
  743. if (list_empty(&drv_data->queue) || !drv_data->running) {
  744. /* pumper kicked off but no work to do */
  745. drv_data->busy = 0;
  746. spin_unlock_irqrestore(&drv_data->lock, flags);
  747. return;
  748. }
  749. /* Make sure we are not already running a message */
  750. if (drv_data->cur_msg) {
  751. spin_unlock_irqrestore(&drv_data->lock, flags);
  752. return;
  753. }
  754. /* Extract head of queue */
  755. drv_data->cur_msg = list_entry(drv_data->queue.next,
  756. struct spi_message, queue);
  757. /* Setup the SSP using the per chip configuration */
  758. drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
  759. bfin_spi_restore_state(drv_data);
  760. list_del_init(&drv_data->cur_msg->queue);
  761. /* Initial message state */
  762. drv_data->cur_msg->state = START_STATE;
  763. drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
  764. struct spi_transfer, transfer_list);
  765. dev_dbg(&drv_data->pdev->dev,
  766. "got a message to pump, state is set to: baud "
  767. "%d, flag 0x%x, ctl 0x%x\n",
  768. drv_data->cur_chip->baud, drv_data->cur_chip->flag,
  769. drv_data->cur_chip->ctl_reg);
  770. dev_dbg(&drv_data->pdev->dev,
  771. "the first transfer len is %d\n",
  772. drv_data->cur_transfer->len);
  773. /* Mark as busy and launch transfers */
  774. tasklet_schedule(&drv_data->pump_transfers);
  775. drv_data->busy = 1;
  776. spin_unlock_irqrestore(&drv_data->lock, flags);
  777. }
  778. /*
  779. * got a msg to transfer, queue it in drv_data->queue.
  780. * And kick off message pumper
  781. */
  782. static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  783. {
  784. struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
  785. unsigned long flags;
  786. spin_lock_irqsave(&drv_data->lock, flags);
  787. if (!drv_data->running) {
  788. spin_unlock_irqrestore(&drv_data->lock, flags);
  789. return -ESHUTDOWN;
  790. }
  791. msg->actual_length = 0;
  792. msg->status = -EINPROGRESS;
  793. msg->state = START_STATE;
  794. dev_dbg(&spi->dev, "adding an msg in transfer() \n");
  795. list_add_tail(&msg->queue, &drv_data->queue);
  796. if (drv_data->running && !drv_data->busy)
  797. queue_work(drv_data->workqueue, &drv_data->pump_messages);
  798. spin_unlock_irqrestore(&drv_data->lock, flags);
  799. return 0;
  800. }
  801. #define MAX_SPI_SSEL 7
  802. static const u16 ssel[][MAX_SPI_SSEL] = {
  803. {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
  804. P_SPI0_SSEL4, P_SPI0_SSEL5,
  805. P_SPI0_SSEL6, P_SPI0_SSEL7},
  806. {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
  807. P_SPI1_SSEL4, P_SPI1_SSEL5,
  808. P_SPI1_SSEL6, P_SPI1_SSEL7},
  809. {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
  810. P_SPI2_SSEL4, P_SPI2_SSEL5,
  811. P_SPI2_SSEL6, P_SPI2_SSEL7},
  812. };
  813. /* setup for devices (may be called multiple times -- not just first setup) */
  814. static int bfin_spi_setup(struct spi_device *spi)
  815. {
  816. struct bfin5xx_spi_chip *chip_info;
  817. struct bfin_spi_slave_data *chip = NULL;
  818. struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
  819. u16 bfin_ctl_reg;
  820. int ret = -EINVAL;
  821. /* Only alloc (or use chip_info) on first setup */
  822. chip_info = NULL;
  823. chip = spi_get_ctldata(spi);
  824. if (chip == NULL) {
  825. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  826. if (!chip) {
  827. dev_err(&spi->dev, "cannot allocate chip data\n");
  828. ret = -ENOMEM;
  829. goto error;
  830. }
  831. chip->enable_dma = 0;
  832. chip_info = spi->controller_data;
  833. }
  834. /* Let people set non-standard bits directly */
  835. bfin_ctl_reg = BIT_CTL_OPENDRAIN | BIT_CTL_EMISO |
  836. BIT_CTL_PSSE | BIT_CTL_GM | BIT_CTL_SZ;
  837. /* chip_info isn't always needed */
  838. if (chip_info) {
  839. /* Make sure people stop trying to set fields via ctl_reg
  840. * when they should actually be using common SPI framework.
  841. * Currently we let through: WOM EMISO PSSE GM SZ.
  842. * Not sure if a user actually needs/uses any of these,
  843. * but let's assume (for now) they do.
  844. */
  845. if (chip_info->ctl_reg & ~bfin_ctl_reg) {
  846. dev_err(&spi->dev,
  847. "do not set bits in ctl_reg that the SPI framework manages\n");
  848. goto error;
  849. }
  850. chip->enable_dma = chip_info->enable_dma != 0
  851. && drv_data->master_info->enable_dma;
  852. chip->ctl_reg = chip_info->ctl_reg;
  853. chip->cs_chg_udelay = chip_info->cs_chg_udelay;
  854. chip->idle_tx_val = chip_info->idle_tx_val;
  855. chip->pio_interrupt = chip_info->pio_interrupt;
  856. } else {
  857. /* force a default base state */
  858. chip->ctl_reg &= bfin_ctl_reg;
  859. }
  860. /* translate common spi framework into our register */
  861. if (spi->mode & SPI_CPOL)
  862. chip->ctl_reg |= BIT_CTL_CPOL;
  863. if (spi->mode & SPI_CPHA)
  864. chip->ctl_reg |= BIT_CTL_CPHA;
  865. if (spi->mode & SPI_LSB_FIRST)
  866. chip->ctl_reg |= BIT_CTL_LSBF;
  867. /* we dont support running in slave mode (yet?) */
  868. chip->ctl_reg |= BIT_CTL_MASTER;
  869. /*
  870. * Notice: for blackfin, the speed_hz is the value of register
  871. * SPI_BAUD, not the real baudrate
  872. */
  873. chip->baud = hz_to_spi_baud(spi->max_speed_hz);
  874. chip->chip_select_num = spi->chip_select;
  875. if (chip->chip_select_num < MAX_CTRL_CS) {
  876. if (!(spi->mode & SPI_CPHA))
  877. dev_warn(&spi->dev,
  878. "Warning: SPI CPHA not set: Slave Select not under software control!\n"
  879. "See Documentation/blackfin/bfin-spi-notes.txt\n");
  880. chip->flag = (1 << spi->chip_select) << 8;
  881. } else
  882. chip->cs_gpio = chip->chip_select_num - MAX_CTRL_CS;
  883. if (chip->enable_dma && chip->pio_interrupt) {
  884. dev_err(&spi->dev,
  885. "enable_dma is set, do not set pio_interrupt\n");
  886. goto error;
  887. }
  888. /*
  889. * if any one SPI chip is registered and wants DMA, request the
  890. * DMA channel for it
  891. */
  892. if (chip->enable_dma && !drv_data->dma_requested) {
  893. /* register dma irq handler */
  894. ret = request_dma(drv_data->dma_channel, "BFIN_SPI_DMA");
  895. if (ret) {
  896. dev_err(&spi->dev,
  897. "Unable to request BlackFin SPI DMA channel\n");
  898. goto error;
  899. }
  900. drv_data->dma_requested = 1;
  901. ret = set_dma_callback(drv_data->dma_channel,
  902. bfin_spi_dma_irq_handler, drv_data);
  903. if (ret) {
  904. dev_err(&spi->dev, "Unable to set dma callback\n");
  905. goto error;
  906. }
  907. dma_disable_irq(drv_data->dma_channel);
  908. }
  909. if (chip->pio_interrupt && !drv_data->irq_requested) {
  910. ret = request_irq(drv_data->spi_irq, bfin_spi_pio_irq_handler,
  911. 0, "BFIN_SPI", drv_data);
  912. if (ret) {
  913. dev_err(&spi->dev, "Unable to register spi IRQ\n");
  914. goto error;
  915. }
  916. drv_data->irq_requested = 1;
  917. /* we use write mode, spi irq has to be disabled here */
  918. disable_irq(drv_data->spi_irq);
  919. }
  920. if (chip->chip_select_num >= MAX_CTRL_CS) {
  921. /* Only request on first setup */
  922. if (spi_get_ctldata(spi) == NULL) {
  923. ret = gpio_request(chip->cs_gpio, spi->modalias);
  924. if (ret) {
  925. dev_err(&spi->dev, "gpio_request() error\n");
  926. goto pin_error;
  927. }
  928. gpio_direction_output(chip->cs_gpio, 1);
  929. }
  930. }
  931. dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
  932. spi->modalias, spi->bits_per_word, chip->enable_dma);
  933. dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
  934. chip->ctl_reg, chip->flag);
  935. spi_set_ctldata(spi, chip);
  936. dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
  937. if (chip->chip_select_num < MAX_CTRL_CS) {
  938. ret = peripheral_request(ssel[spi->master->bus_num]
  939. [chip->chip_select_num-1], spi->modalias);
  940. if (ret) {
  941. dev_err(&spi->dev, "peripheral_request() error\n");
  942. goto pin_error;
  943. }
  944. }
  945. bfin_spi_cs_enable(drv_data, chip);
  946. bfin_spi_cs_deactive(drv_data, chip);
  947. return 0;
  948. pin_error:
  949. if (chip->chip_select_num >= MAX_CTRL_CS)
  950. gpio_free(chip->cs_gpio);
  951. else
  952. peripheral_free(ssel[spi->master->bus_num]
  953. [chip->chip_select_num - 1]);
  954. error:
  955. if (chip) {
  956. if (drv_data->dma_requested)
  957. free_dma(drv_data->dma_channel);
  958. drv_data->dma_requested = 0;
  959. kfree(chip);
  960. /* prevent free 'chip' twice */
  961. spi_set_ctldata(spi, NULL);
  962. }
  963. return ret;
  964. }
  965. /*
  966. * callback for spi framework.
  967. * clean driver specific data
  968. */
  969. static void bfin_spi_cleanup(struct spi_device *spi)
  970. {
  971. struct bfin_spi_slave_data *chip = spi_get_ctldata(spi);
  972. struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
  973. if (!chip)
  974. return;
  975. if (chip->chip_select_num < MAX_CTRL_CS) {
  976. peripheral_free(ssel[spi->master->bus_num]
  977. [chip->chip_select_num-1]);
  978. bfin_spi_cs_disable(drv_data, chip);
  979. } else
  980. gpio_free(chip->cs_gpio);
  981. kfree(chip);
  982. /* prevent free 'chip' twice */
  983. spi_set_ctldata(spi, NULL);
  984. }
  985. static int bfin_spi_init_queue(struct bfin_spi_master_data *drv_data)
  986. {
  987. INIT_LIST_HEAD(&drv_data->queue);
  988. spin_lock_init(&drv_data->lock);
  989. drv_data->running = false;
  990. drv_data->busy = 0;
  991. /* init transfer tasklet */
  992. tasklet_init(&drv_data->pump_transfers,
  993. bfin_spi_pump_transfers, (unsigned long)drv_data);
  994. /* init messages workqueue */
  995. INIT_WORK(&drv_data->pump_messages, bfin_spi_pump_messages);
  996. drv_data->workqueue = create_singlethread_workqueue(
  997. dev_name(drv_data->master->dev.parent));
  998. if (drv_data->workqueue == NULL)
  999. return -EBUSY;
  1000. return 0;
  1001. }
  1002. static int bfin_spi_start_queue(struct bfin_spi_master_data *drv_data)
  1003. {
  1004. unsigned long flags;
  1005. spin_lock_irqsave(&drv_data->lock, flags);
  1006. if (drv_data->running || drv_data->busy) {
  1007. spin_unlock_irqrestore(&drv_data->lock, flags);
  1008. return -EBUSY;
  1009. }
  1010. drv_data->running = true;
  1011. drv_data->cur_msg = NULL;
  1012. drv_data->cur_transfer = NULL;
  1013. drv_data->cur_chip = NULL;
  1014. spin_unlock_irqrestore(&drv_data->lock, flags);
  1015. queue_work(drv_data->workqueue, &drv_data->pump_messages);
  1016. return 0;
  1017. }
  1018. static int bfin_spi_stop_queue(struct bfin_spi_master_data *drv_data)
  1019. {
  1020. unsigned long flags;
  1021. unsigned limit = 500;
  1022. int status = 0;
  1023. spin_lock_irqsave(&drv_data->lock, flags);
  1024. /*
  1025. * This is a bit lame, but is optimized for the common execution path.
  1026. * A wait_queue on the drv_data->busy could be used, but then the common
  1027. * execution path (pump_messages) would be required to call wake_up or
  1028. * friends on every SPI message. Do this instead
  1029. */
  1030. drv_data->running = false;
  1031. while ((!list_empty(&drv_data->queue) || drv_data->busy) && limit--) {
  1032. spin_unlock_irqrestore(&drv_data->lock, flags);
  1033. msleep(10);
  1034. spin_lock_irqsave(&drv_data->lock, flags);
  1035. }
  1036. if (!list_empty(&drv_data->queue) || drv_data->busy)
  1037. status = -EBUSY;
  1038. spin_unlock_irqrestore(&drv_data->lock, flags);
  1039. return status;
  1040. }
  1041. static int bfin_spi_destroy_queue(struct bfin_spi_master_data *drv_data)
  1042. {
  1043. int status;
  1044. status = bfin_spi_stop_queue(drv_data);
  1045. if (status != 0)
  1046. return status;
  1047. destroy_workqueue(drv_data->workqueue);
  1048. return 0;
  1049. }
  1050. static int bfin_spi_probe(struct platform_device *pdev)
  1051. {
  1052. struct device *dev = &pdev->dev;
  1053. struct bfin5xx_spi_master *platform_info;
  1054. struct spi_master *master;
  1055. struct bfin_spi_master_data *drv_data;
  1056. struct resource *res;
  1057. int status = 0;
  1058. platform_info = dev_get_platdata(dev);
  1059. /* Allocate master with space for drv_data */
  1060. master = spi_alloc_master(dev, sizeof(*drv_data));
  1061. if (!master) {
  1062. dev_err(&pdev->dev, "can not alloc spi_master\n");
  1063. return -ENOMEM;
  1064. }
  1065. drv_data = spi_master_get_devdata(master);
  1066. drv_data->master = master;
  1067. drv_data->master_info = platform_info;
  1068. drv_data->pdev = pdev;
  1069. drv_data->pin_req = platform_info->pin_req;
  1070. /* the spi->mode bits supported by this driver: */
  1071. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
  1072. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  1073. master->bus_num = pdev->id;
  1074. master->num_chipselect = platform_info->num_chipselect;
  1075. master->cleanup = bfin_spi_cleanup;
  1076. master->setup = bfin_spi_setup;
  1077. master->transfer = bfin_spi_transfer;
  1078. /* Find and map our resources */
  1079. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1080. if (res == NULL) {
  1081. dev_err(dev, "Cannot get IORESOURCE_MEM\n");
  1082. status = -ENOENT;
  1083. goto out_error_get_res;
  1084. }
  1085. drv_data->regs = ioremap(res->start, resource_size(res));
  1086. if (drv_data->regs == NULL) {
  1087. dev_err(dev, "Cannot map IO\n");
  1088. status = -ENXIO;
  1089. goto out_error_ioremap;
  1090. }
  1091. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  1092. if (res == NULL) {
  1093. dev_err(dev, "No DMA channel specified\n");
  1094. status = -ENOENT;
  1095. goto out_error_free_io;
  1096. }
  1097. drv_data->dma_channel = res->start;
  1098. drv_data->spi_irq = platform_get_irq(pdev, 0);
  1099. if (drv_data->spi_irq < 0) {
  1100. dev_err(dev, "No spi pio irq specified\n");
  1101. status = -ENOENT;
  1102. goto out_error_free_io;
  1103. }
  1104. /* Initial and start queue */
  1105. status = bfin_spi_init_queue(drv_data);
  1106. if (status != 0) {
  1107. dev_err(dev, "problem initializing queue\n");
  1108. goto out_error_queue_alloc;
  1109. }
  1110. status = bfin_spi_start_queue(drv_data);
  1111. if (status != 0) {
  1112. dev_err(dev, "problem starting queue\n");
  1113. goto out_error_queue_alloc;
  1114. }
  1115. status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
  1116. if (status != 0) {
  1117. dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
  1118. goto out_error_queue_alloc;
  1119. }
  1120. /* Reset SPI registers. If these registers were used by the boot loader,
  1121. * the sky may fall on your head if you enable the dma controller.
  1122. */
  1123. bfin_write(&drv_data->regs->ctl, BIT_CTL_CPHA | BIT_CTL_MASTER);
  1124. bfin_write(&drv_data->regs->flg, 0xFF00);
  1125. /* Register with the SPI framework */
  1126. platform_set_drvdata(pdev, drv_data);
  1127. status = spi_register_master(master);
  1128. if (status != 0) {
  1129. dev_err(dev, "problem registering spi master\n");
  1130. goto out_error_queue_alloc;
  1131. }
  1132. dev_info(dev, "%s, Version %s, regs@%p, dma channel@%d\n",
  1133. DRV_DESC, DRV_VERSION, drv_data->regs,
  1134. drv_data->dma_channel);
  1135. return status;
  1136. out_error_queue_alloc:
  1137. bfin_spi_destroy_queue(drv_data);
  1138. out_error_free_io:
  1139. iounmap(drv_data->regs);
  1140. out_error_ioremap:
  1141. out_error_get_res:
  1142. spi_master_put(master);
  1143. return status;
  1144. }
  1145. /* stop hardware and remove the driver */
  1146. static int bfin_spi_remove(struct platform_device *pdev)
  1147. {
  1148. struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev);
  1149. int status = 0;
  1150. if (!drv_data)
  1151. return 0;
  1152. /* Remove the queue */
  1153. status = bfin_spi_destroy_queue(drv_data);
  1154. if (status != 0)
  1155. return status;
  1156. /* Disable the SSP at the peripheral and SOC level */
  1157. bfin_spi_disable(drv_data);
  1158. /* Release DMA */
  1159. if (drv_data->master_info->enable_dma) {
  1160. if (dma_channel_active(drv_data->dma_channel))
  1161. free_dma(drv_data->dma_channel);
  1162. }
  1163. if (drv_data->irq_requested) {
  1164. free_irq(drv_data->spi_irq, drv_data);
  1165. drv_data->irq_requested = 0;
  1166. }
  1167. /* Disconnect from the SPI framework */
  1168. spi_unregister_master(drv_data->master);
  1169. peripheral_free_list(drv_data->pin_req);
  1170. return 0;
  1171. }
  1172. #ifdef CONFIG_PM_SLEEP
  1173. static int bfin_spi_suspend(struct device *dev)
  1174. {
  1175. struct bfin_spi_master_data *drv_data = dev_get_drvdata(dev);
  1176. int status = 0;
  1177. status = bfin_spi_stop_queue(drv_data);
  1178. if (status != 0)
  1179. return status;
  1180. drv_data->ctrl_reg = bfin_read(&drv_data->regs->ctl);
  1181. drv_data->flag_reg = bfin_read(&drv_data->regs->flg);
  1182. /*
  1183. * reset SPI_CTL and SPI_FLG registers
  1184. */
  1185. bfin_write(&drv_data->regs->ctl, BIT_CTL_CPHA | BIT_CTL_MASTER);
  1186. bfin_write(&drv_data->regs->flg, 0xFF00);
  1187. return 0;
  1188. }
  1189. static int bfin_spi_resume(struct device *dev)
  1190. {
  1191. struct bfin_spi_master_data *drv_data = dev_get_drvdata(dev);
  1192. int status = 0;
  1193. bfin_write(&drv_data->regs->ctl, drv_data->ctrl_reg);
  1194. bfin_write(&drv_data->regs->flg, drv_data->flag_reg);
  1195. /* Start the queue running */
  1196. status = bfin_spi_start_queue(drv_data);
  1197. if (status != 0) {
  1198. dev_err(dev, "problem starting queue (%d)\n", status);
  1199. return status;
  1200. }
  1201. return 0;
  1202. }
  1203. static SIMPLE_DEV_PM_OPS(bfin_spi_pm_ops, bfin_spi_suspend, bfin_spi_resume);
  1204. #define BFIN_SPI_PM_OPS (&bfin_spi_pm_ops)
  1205. #else
  1206. #define BFIN_SPI_PM_OPS NULL
  1207. #endif
  1208. MODULE_ALIAS("platform:bfin-spi");
  1209. static struct platform_driver bfin_spi_driver = {
  1210. .driver = {
  1211. .name = DRV_NAME,
  1212. .pm = BFIN_SPI_PM_OPS,
  1213. },
  1214. .probe = bfin_spi_probe,
  1215. .remove = bfin_spi_remove,
  1216. };
  1217. static int __init bfin_spi_init(void)
  1218. {
  1219. return platform_driver_register(&bfin_spi_driver);
  1220. }
  1221. subsys_initcall(bfin_spi_init);
  1222. static void __exit bfin_spi_exit(void)
  1223. {
  1224. platform_driver_unregister(&bfin_spi_driver);
  1225. }
  1226. module_exit(bfin_spi_exit);