spi-bfin5xx.c 41 KB

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