spi-bfin5xx.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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. bfin_write(&drv_data->regs->baud, hz_to_spi_baud(transfer->speed_hz));
  562. bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
  563. bfin_spi_cs_active(drv_data, chip);
  564. dev_dbg(&drv_data->pdev->dev,
  565. "now pumping a transfer: width is %d, len is %d\n",
  566. cr_width, transfer->len);
  567. /*
  568. * Try to map dma buffer and do a dma transfer. If successful use,
  569. * different way to r/w according to the enable_dma settings and if
  570. * we are not doing a full duplex transfer (since the hardware does
  571. * not support full duplex DMA transfers).
  572. */
  573. if (!full_duplex && drv_data->cur_chip->enable_dma
  574. && drv_data->len > 6) {
  575. unsigned long dma_start_addr, flags;
  576. disable_dma(drv_data->dma_channel);
  577. clear_dma_irqstat(drv_data->dma_channel);
  578. /* config dma channel */
  579. dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
  580. set_dma_x_count(drv_data->dma_channel, drv_data->len);
  581. if (cr_width == BIT_CTL_WORDSIZE) {
  582. set_dma_x_modify(drv_data->dma_channel, 2);
  583. dma_width = WDSIZE_16;
  584. } else {
  585. set_dma_x_modify(drv_data->dma_channel, 1);
  586. dma_width = WDSIZE_8;
  587. }
  588. /* poll for SPI completion before start */
  589. while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF))
  590. cpu_relax();
  591. /* dirty hack for autobuffer DMA mode */
  592. if (drv_data->tx_dma == 0xFFFF) {
  593. dev_dbg(&drv_data->pdev->dev,
  594. "doing autobuffer DMA out.\n");
  595. /* no irq in autobuffer mode */
  596. dma_config =
  597. (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
  598. set_dma_config(drv_data->dma_channel, dma_config);
  599. set_dma_start_addr(drv_data->dma_channel,
  600. (unsigned long)drv_data->tx);
  601. enable_dma(drv_data->dma_channel);
  602. /* start SPI transfer */
  603. bfin_write(&drv_data->regs->ctl, cr | BIT_CTL_TIMOD_DMA_TX);
  604. /* just return here, there can only be one transfer
  605. * in this mode
  606. */
  607. message->status = 0;
  608. bfin_spi_giveback(drv_data);
  609. return;
  610. }
  611. /* In dma mode, rx or tx must be NULL in one transfer */
  612. dma_config = (RESTART | dma_width | DI_EN);
  613. if (drv_data->rx != NULL) {
  614. /* set transfer mode, and enable SPI */
  615. dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n",
  616. drv_data->rx, drv_data->len_in_bytes);
  617. /* invalidate caches, if needed */
  618. if (bfin_addr_dcacheable((unsigned long) drv_data->rx))
  619. invalidate_dcache_range((unsigned long) drv_data->rx,
  620. (unsigned long) (drv_data->rx +
  621. drv_data->len_in_bytes));
  622. dma_config |= WNR;
  623. dma_start_addr = (unsigned long)drv_data->rx;
  624. cr |= BIT_CTL_TIMOD_DMA_RX | BIT_CTL_SENDOPT;
  625. } else if (drv_data->tx != NULL) {
  626. dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
  627. /* flush caches, if needed */
  628. if (bfin_addr_dcacheable((unsigned long) drv_data->tx))
  629. flush_dcache_range((unsigned long) drv_data->tx,
  630. (unsigned long) (drv_data->tx +
  631. drv_data->len_in_bytes));
  632. dma_start_addr = (unsigned long)drv_data->tx;
  633. cr |= BIT_CTL_TIMOD_DMA_TX;
  634. } else
  635. BUG();
  636. /* oh man, here there be monsters ... and i dont mean the
  637. * fluffy cute ones from pixar, i mean the kind that'll eat
  638. * your data, kick your dog, and love it all. do *not* try
  639. * and change these lines unless you (1) heavily test DMA
  640. * with SPI flashes on a loaded system (e.g. ping floods),
  641. * (2) know just how broken the DMA engine interaction with
  642. * the SPI peripheral is, and (3) have someone else to blame
  643. * when you screw it all up anyways.
  644. */
  645. set_dma_start_addr(drv_data->dma_channel, dma_start_addr);
  646. set_dma_config(drv_data->dma_channel, dma_config);
  647. local_irq_save(flags);
  648. SSYNC();
  649. bfin_write(&drv_data->regs->ctl, cr);
  650. enable_dma(drv_data->dma_channel);
  651. dma_enable_irq(drv_data->dma_channel);
  652. local_irq_restore(flags);
  653. return;
  654. }
  655. /*
  656. * We always use SPI_WRITE mode (transfer starts with TDBR write).
  657. * SPI_READ mode (transfer starts with RDBR read) seems to have
  658. * problems with setting up the output value in TDBR prior to the
  659. * start of the transfer.
  660. */
  661. bfin_write(&drv_data->regs->ctl, cr | BIT_CTL_TXMOD);
  662. if (chip->pio_interrupt) {
  663. /* SPI irq should have been disabled by now */
  664. /* discard old RX data and clear RXS */
  665. bfin_spi_dummy_read(drv_data);
  666. /* start transfer */
  667. if (drv_data->tx == NULL)
  668. bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
  669. else {
  670. int loop;
  671. if (bits_per_word == 16) {
  672. u16 *buf = (u16 *)drv_data->tx;
  673. for (loop = 0; loop < bits_per_word / 16;
  674. loop++) {
  675. bfin_write(&drv_data->regs->tdbr, *buf++);
  676. }
  677. } else if (bits_per_word == 8) {
  678. u8 *buf = (u8 *)drv_data->tx;
  679. for (loop = 0; loop < bits_per_word / 8; loop++)
  680. bfin_write(&drv_data->regs->tdbr, *buf++);
  681. }
  682. drv_data->tx += drv_data->n_bytes;
  683. }
  684. /* once TDBR is empty, interrupt is triggered */
  685. enable_irq(drv_data->spi_irq);
  686. return;
  687. }
  688. /* IO mode */
  689. dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
  690. if (full_duplex) {
  691. /* full duplex mode */
  692. BUG_ON((drv_data->tx_end - drv_data->tx) !=
  693. (drv_data->rx_end - drv_data->rx));
  694. dev_dbg(&drv_data->pdev->dev,
  695. "IO duplex: cr is 0x%x\n", cr);
  696. drv_data->ops->duplex(drv_data);
  697. if (drv_data->tx != drv_data->tx_end)
  698. tranf_success = 0;
  699. } else if (drv_data->tx != NULL) {
  700. /* write only half duplex */
  701. dev_dbg(&drv_data->pdev->dev,
  702. "IO write: cr is 0x%x\n", cr);
  703. drv_data->ops->write(drv_data);
  704. if (drv_data->tx != drv_data->tx_end)
  705. tranf_success = 0;
  706. } else if (drv_data->rx != NULL) {
  707. /* read only half duplex */
  708. dev_dbg(&drv_data->pdev->dev,
  709. "IO read: cr is 0x%x\n", cr);
  710. drv_data->ops->read(drv_data);
  711. if (drv_data->rx != drv_data->rx_end)
  712. tranf_success = 0;
  713. }
  714. if (!tranf_success) {
  715. dev_dbg(&drv_data->pdev->dev,
  716. "IO write error!\n");
  717. message->state = ERROR_STATE;
  718. } else {
  719. /* Update total byte transferred */
  720. message->actual_length += drv_data->len_in_bytes;
  721. /* Move to next transfer of this msg */
  722. message->state = bfin_spi_next_transfer(drv_data);
  723. if (drv_data->cs_change && message->state != DONE_STATE) {
  724. bfin_spi_flush(drv_data);
  725. bfin_spi_cs_deactive(drv_data, chip);
  726. }
  727. }
  728. /* Schedule next transfer tasklet */
  729. tasklet_schedule(&drv_data->pump_transfers);
  730. }
  731. /* pop a msg from queue and kick off real transfer */
  732. static void bfin_spi_pump_messages(struct work_struct *work)
  733. {
  734. struct bfin_spi_master_data *drv_data;
  735. unsigned long flags;
  736. drv_data = container_of(work, struct bfin_spi_master_data, pump_messages);
  737. /* Lock queue and check for queue work */
  738. spin_lock_irqsave(&drv_data->lock, flags);
  739. if (list_empty(&drv_data->queue) || !drv_data->running) {
  740. /* pumper kicked off but no work to do */
  741. drv_data->busy = 0;
  742. spin_unlock_irqrestore(&drv_data->lock, flags);
  743. return;
  744. }
  745. /* Make sure we are not already running a message */
  746. if (drv_data->cur_msg) {
  747. spin_unlock_irqrestore(&drv_data->lock, flags);
  748. return;
  749. }
  750. /* Extract head of queue */
  751. drv_data->cur_msg = list_entry(drv_data->queue.next,
  752. struct spi_message, queue);
  753. /* Setup the SSP using the per chip configuration */
  754. drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
  755. bfin_spi_restore_state(drv_data);
  756. list_del_init(&drv_data->cur_msg->queue);
  757. /* Initial message state */
  758. drv_data->cur_msg->state = START_STATE;
  759. drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
  760. struct spi_transfer, transfer_list);
  761. dev_dbg(&drv_data->pdev->dev,
  762. "got a message to pump, state is set to: baud "
  763. "%d, flag 0x%x, ctl 0x%x\n",
  764. drv_data->cur_chip->baud, drv_data->cur_chip->flag,
  765. drv_data->cur_chip->ctl_reg);
  766. dev_dbg(&drv_data->pdev->dev,
  767. "the first transfer len is %d\n",
  768. drv_data->cur_transfer->len);
  769. /* Mark as busy and launch transfers */
  770. tasklet_schedule(&drv_data->pump_transfers);
  771. drv_data->busy = 1;
  772. spin_unlock_irqrestore(&drv_data->lock, flags);
  773. }
  774. /*
  775. * got a msg to transfer, queue it in drv_data->queue.
  776. * And kick off message pumper
  777. */
  778. static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  779. {
  780. struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
  781. unsigned long flags;
  782. spin_lock_irqsave(&drv_data->lock, flags);
  783. if (!drv_data->running) {
  784. spin_unlock_irqrestore(&drv_data->lock, flags);
  785. return -ESHUTDOWN;
  786. }
  787. msg->actual_length = 0;
  788. msg->status = -EINPROGRESS;
  789. msg->state = START_STATE;
  790. dev_dbg(&spi->dev, "adding an msg in transfer() \n");
  791. list_add_tail(&msg->queue, &drv_data->queue);
  792. if (drv_data->running && !drv_data->busy)
  793. queue_work(drv_data->workqueue, &drv_data->pump_messages);
  794. spin_unlock_irqrestore(&drv_data->lock, flags);
  795. return 0;
  796. }
  797. #define MAX_SPI_SSEL 7
  798. static const u16 ssel[][MAX_SPI_SSEL] = {
  799. {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
  800. P_SPI0_SSEL4, P_SPI0_SSEL5,
  801. P_SPI0_SSEL6, P_SPI0_SSEL7},
  802. {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
  803. P_SPI1_SSEL4, P_SPI1_SSEL5,
  804. P_SPI1_SSEL6, P_SPI1_SSEL7},
  805. {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
  806. P_SPI2_SSEL4, P_SPI2_SSEL5,
  807. P_SPI2_SSEL6, P_SPI2_SSEL7},
  808. };
  809. /* setup for devices (may be called multiple times -- not just first setup) */
  810. static int bfin_spi_setup(struct spi_device *spi)
  811. {
  812. struct bfin5xx_spi_chip *chip_info;
  813. struct bfin_spi_slave_data *chip = NULL;
  814. struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
  815. u16 bfin_ctl_reg;
  816. int ret = -EINVAL;
  817. /* Only alloc (or use chip_info) on first setup */
  818. chip_info = NULL;
  819. chip = spi_get_ctldata(spi);
  820. if (chip == NULL) {
  821. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  822. if (!chip) {
  823. dev_err(&spi->dev, "cannot allocate chip data\n");
  824. ret = -ENOMEM;
  825. goto error;
  826. }
  827. chip->enable_dma = 0;
  828. chip_info = spi->controller_data;
  829. }
  830. /* Let people set non-standard bits directly */
  831. bfin_ctl_reg = BIT_CTL_OPENDRAIN | BIT_CTL_EMISO |
  832. BIT_CTL_PSSE | BIT_CTL_GM | BIT_CTL_SZ;
  833. /* chip_info isn't always needed */
  834. if (chip_info) {
  835. /* Make sure people stop trying to set fields via ctl_reg
  836. * when they should actually be using common SPI framework.
  837. * Currently we let through: WOM EMISO PSSE GM SZ.
  838. * Not sure if a user actually needs/uses any of these,
  839. * but let's assume (for now) they do.
  840. */
  841. if (chip_info->ctl_reg & ~bfin_ctl_reg) {
  842. dev_err(&spi->dev,
  843. "do not set bits in ctl_reg that the SPI framework manages\n");
  844. goto error;
  845. }
  846. chip->enable_dma = chip_info->enable_dma != 0
  847. && drv_data->master_info->enable_dma;
  848. chip->ctl_reg = chip_info->ctl_reg;
  849. chip->cs_chg_udelay = chip_info->cs_chg_udelay;
  850. chip->idle_tx_val = chip_info->idle_tx_val;
  851. chip->pio_interrupt = chip_info->pio_interrupt;
  852. } else {
  853. /* force a default base state */
  854. chip->ctl_reg &= bfin_ctl_reg;
  855. }
  856. /* translate common spi framework into our register */
  857. if (spi->mode & SPI_CPOL)
  858. chip->ctl_reg |= BIT_CTL_CPOL;
  859. if (spi->mode & SPI_CPHA)
  860. chip->ctl_reg |= BIT_CTL_CPHA;
  861. if (spi->mode & SPI_LSB_FIRST)
  862. chip->ctl_reg |= BIT_CTL_LSBF;
  863. /* we dont support running in slave mode (yet?) */
  864. chip->ctl_reg |= BIT_CTL_MASTER;
  865. /*
  866. * Notice: for blackfin, the speed_hz is the value of register
  867. * SPI_BAUD, not the real baudrate
  868. */
  869. chip->baud = hz_to_spi_baud(spi->max_speed_hz);
  870. chip->chip_select_num = spi->chip_select;
  871. if (chip->chip_select_num < MAX_CTRL_CS) {
  872. if (!(spi->mode & SPI_CPHA))
  873. dev_warn(&spi->dev,
  874. "Warning: SPI CPHA not set: Slave Select not under software control!\n"
  875. "See Documentation/blackfin/bfin-spi-notes.txt\n");
  876. chip->flag = (1 << spi->chip_select) << 8;
  877. } else
  878. chip->cs_gpio = chip->chip_select_num - MAX_CTRL_CS;
  879. if (chip->enable_dma && chip->pio_interrupt) {
  880. dev_err(&spi->dev,
  881. "enable_dma is set, do not set pio_interrupt\n");
  882. goto error;
  883. }
  884. /*
  885. * if any one SPI chip is registered and wants DMA, request the
  886. * DMA channel for it
  887. */
  888. if (chip->enable_dma && !drv_data->dma_requested) {
  889. /* register dma irq handler */
  890. ret = request_dma(drv_data->dma_channel, "BFIN_SPI_DMA");
  891. if (ret) {
  892. dev_err(&spi->dev,
  893. "Unable to request BlackFin SPI DMA channel\n");
  894. goto error;
  895. }
  896. drv_data->dma_requested = 1;
  897. ret = set_dma_callback(drv_data->dma_channel,
  898. bfin_spi_dma_irq_handler, drv_data);
  899. if (ret) {
  900. dev_err(&spi->dev, "Unable to set dma callback\n");
  901. goto error;
  902. }
  903. dma_disable_irq(drv_data->dma_channel);
  904. }
  905. if (chip->pio_interrupt && !drv_data->irq_requested) {
  906. ret = request_irq(drv_data->spi_irq, bfin_spi_pio_irq_handler,
  907. 0, "BFIN_SPI", drv_data);
  908. if (ret) {
  909. dev_err(&spi->dev, "Unable to register spi IRQ\n");
  910. goto error;
  911. }
  912. drv_data->irq_requested = 1;
  913. /* we use write mode, spi irq has to be disabled here */
  914. disable_irq(drv_data->spi_irq);
  915. }
  916. if (chip->chip_select_num >= MAX_CTRL_CS) {
  917. /* Only request on first setup */
  918. if (spi_get_ctldata(spi) == NULL) {
  919. ret = gpio_request(chip->cs_gpio, spi->modalias);
  920. if (ret) {
  921. dev_err(&spi->dev, "gpio_request() error\n");
  922. goto pin_error;
  923. }
  924. gpio_direction_output(chip->cs_gpio, 1);
  925. }
  926. }
  927. dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
  928. spi->modalias, spi->bits_per_word, chip->enable_dma);
  929. dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
  930. chip->ctl_reg, chip->flag);
  931. spi_set_ctldata(spi, chip);
  932. dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
  933. if (chip->chip_select_num < MAX_CTRL_CS) {
  934. ret = peripheral_request(ssel[spi->master->bus_num]
  935. [chip->chip_select_num-1], spi->modalias);
  936. if (ret) {
  937. dev_err(&spi->dev, "peripheral_request() error\n");
  938. goto pin_error;
  939. }
  940. }
  941. bfin_spi_cs_enable(drv_data, chip);
  942. bfin_spi_cs_deactive(drv_data, chip);
  943. return 0;
  944. pin_error:
  945. if (chip->chip_select_num >= MAX_CTRL_CS)
  946. gpio_free(chip->cs_gpio);
  947. else
  948. peripheral_free(ssel[spi->master->bus_num]
  949. [chip->chip_select_num - 1]);
  950. error:
  951. if (chip) {
  952. if (drv_data->dma_requested)
  953. free_dma(drv_data->dma_channel);
  954. drv_data->dma_requested = 0;
  955. kfree(chip);
  956. /* prevent free 'chip' twice */
  957. spi_set_ctldata(spi, NULL);
  958. }
  959. return ret;
  960. }
  961. /*
  962. * callback for spi framework.
  963. * clean driver specific data
  964. */
  965. static void bfin_spi_cleanup(struct spi_device *spi)
  966. {
  967. struct bfin_spi_slave_data *chip = spi_get_ctldata(spi);
  968. struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
  969. if (!chip)
  970. return;
  971. if (chip->chip_select_num < MAX_CTRL_CS) {
  972. peripheral_free(ssel[spi->master->bus_num]
  973. [chip->chip_select_num-1]);
  974. bfin_spi_cs_disable(drv_data, chip);
  975. } else
  976. gpio_free(chip->cs_gpio);
  977. kfree(chip);
  978. /* prevent free 'chip' twice */
  979. spi_set_ctldata(spi, NULL);
  980. }
  981. static int bfin_spi_init_queue(struct bfin_spi_master_data *drv_data)
  982. {
  983. INIT_LIST_HEAD(&drv_data->queue);
  984. spin_lock_init(&drv_data->lock);
  985. drv_data->running = false;
  986. drv_data->busy = 0;
  987. /* init transfer tasklet */
  988. tasklet_init(&drv_data->pump_transfers,
  989. bfin_spi_pump_transfers, (unsigned long)drv_data);
  990. /* init messages workqueue */
  991. INIT_WORK(&drv_data->pump_messages, bfin_spi_pump_messages);
  992. drv_data->workqueue = create_singlethread_workqueue(
  993. dev_name(drv_data->master->dev.parent));
  994. if (drv_data->workqueue == NULL)
  995. return -EBUSY;
  996. return 0;
  997. }
  998. static int bfin_spi_start_queue(struct bfin_spi_master_data *drv_data)
  999. {
  1000. unsigned long flags;
  1001. spin_lock_irqsave(&drv_data->lock, flags);
  1002. if (drv_data->running || drv_data->busy) {
  1003. spin_unlock_irqrestore(&drv_data->lock, flags);
  1004. return -EBUSY;
  1005. }
  1006. drv_data->running = true;
  1007. drv_data->cur_msg = NULL;
  1008. drv_data->cur_transfer = NULL;
  1009. drv_data->cur_chip = NULL;
  1010. spin_unlock_irqrestore(&drv_data->lock, flags);
  1011. queue_work(drv_data->workqueue, &drv_data->pump_messages);
  1012. return 0;
  1013. }
  1014. static int bfin_spi_stop_queue(struct bfin_spi_master_data *drv_data)
  1015. {
  1016. unsigned long flags;
  1017. unsigned limit = 500;
  1018. int status = 0;
  1019. spin_lock_irqsave(&drv_data->lock, flags);
  1020. /*
  1021. * This is a bit lame, but is optimized for the common execution path.
  1022. * A wait_queue on the drv_data->busy could be used, but then the common
  1023. * execution path (pump_messages) would be required to call wake_up or
  1024. * friends on every SPI message. Do this instead
  1025. */
  1026. drv_data->running = false;
  1027. while ((!list_empty(&drv_data->queue) || drv_data->busy) && limit--) {
  1028. spin_unlock_irqrestore(&drv_data->lock, flags);
  1029. msleep(10);
  1030. spin_lock_irqsave(&drv_data->lock, flags);
  1031. }
  1032. if (!list_empty(&drv_data->queue) || drv_data->busy)
  1033. status = -EBUSY;
  1034. spin_unlock_irqrestore(&drv_data->lock, flags);
  1035. return status;
  1036. }
  1037. static int bfin_spi_destroy_queue(struct bfin_spi_master_data *drv_data)
  1038. {
  1039. int status;
  1040. status = bfin_spi_stop_queue(drv_data);
  1041. if (status != 0)
  1042. return status;
  1043. destroy_workqueue(drv_data->workqueue);
  1044. return 0;
  1045. }
  1046. static int bfin_spi_probe(struct platform_device *pdev)
  1047. {
  1048. struct device *dev = &pdev->dev;
  1049. struct bfin5xx_spi_master *platform_info;
  1050. struct spi_master *master;
  1051. struct bfin_spi_master_data *drv_data;
  1052. struct resource *res;
  1053. int status = 0;
  1054. platform_info = dev_get_platdata(dev);
  1055. /* Allocate master with space for drv_data */
  1056. master = spi_alloc_master(dev, sizeof(*drv_data));
  1057. if (!master) {
  1058. dev_err(&pdev->dev, "can not alloc spi_master\n");
  1059. return -ENOMEM;
  1060. }
  1061. drv_data = spi_master_get_devdata(master);
  1062. drv_data->master = master;
  1063. drv_data->master_info = platform_info;
  1064. drv_data->pdev = pdev;
  1065. drv_data->pin_req = platform_info->pin_req;
  1066. /* the spi->mode bits supported by this driver: */
  1067. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
  1068. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  1069. master->bus_num = pdev->id;
  1070. master->num_chipselect = platform_info->num_chipselect;
  1071. master->cleanup = bfin_spi_cleanup;
  1072. master->setup = bfin_spi_setup;
  1073. master->transfer = bfin_spi_transfer;
  1074. /* Find and map our resources */
  1075. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1076. if (res == NULL) {
  1077. dev_err(dev, "Cannot get IORESOURCE_MEM\n");
  1078. status = -ENOENT;
  1079. goto out_error_get_res;
  1080. }
  1081. drv_data->regs = ioremap(res->start, resource_size(res));
  1082. if (drv_data->regs == NULL) {
  1083. dev_err(dev, "Cannot map IO\n");
  1084. status = -ENXIO;
  1085. goto out_error_ioremap;
  1086. }
  1087. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  1088. if (res == NULL) {
  1089. dev_err(dev, "No DMA channel specified\n");
  1090. status = -ENOENT;
  1091. goto out_error_free_io;
  1092. }
  1093. drv_data->dma_channel = res->start;
  1094. drv_data->spi_irq = platform_get_irq(pdev, 0);
  1095. if (drv_data->spi_irq < 0) {
  1096. dev_err(dev, "No spi pio irq specified\n");
  1097. status = -ENOENT;
  1098. goto out_error_free_io;
  1099. }
  1100. /* Initial and start queue */
  1101. status = bfin_spi_init_queue(drv_data);
  1102. if (status != 0) {
  1103. dev_err(dev, "problem initializing queue\n");
  1104. goto out_error_queue_alloc;
  1105. }
  1106. status = bfin_spi_start_queue(drv_data);
  1107. if (status != 0) {
  1108. dev_err(dev, "problem starting queue\n");
  1109. goto out_error_queue_alloc;
  1110. }
  1111. status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
  1112. if (status != 0) {
  1113. dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
  1114. goto out_error_queue_alloc;
  1115. }
  1116. /* Reset SPI registers. If these registers were used by the boot loader,
  1117. * the sky may fall on your head if you enable the dma controller.
  1118. */
  1119. bfin_write(&drv_data->regs->ctl, BIT_CTL_CPHA | BIT_CTL_MASTER);
  1120. bfin_write(&drv_data->regs->flg, 0xFF00);
  1121. /* Register with the SPI framework */
  1122. platform_set_drvdata(pdev, drv_data);
  1123. status = spi_register_master(master);
  1124. if (status != 0) {
  1125. dev_err(dev, "problem registering spi master\n");
  1126. goto out_error_queue_alloc;
  1127. }
  1128. dev_info(dev, "%s, Version %s, regs@%p, dma channel@%d\n",
  1129. DRV_DESC, DRV_VERSION, drv_data->regs,
  1130. drv_data->dma_channel);
  1131. return status;
  1132. out_error_queue_alloc:
  1133. bfin_spi_destroy_queue(drv_data);
  1134. out_error_free_io:
  1135. iounmap(drv_data->regs);
  1136. out_error_ioremap:
  1137. out_error_get_res:
  1138. spi_master_put(master);
  1139. return status;
  1140. }
  1141. /* stop hardware and remove the driver */
  1142. static int bfin_spi_remove(struct platform_device *pdev)
  1143. {
  1144. struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev);
  1145. int status = 0;
  1146. if (!drv_data)
  1147. return 0;
  1148. /* Remove the queue */
  1149. status = bfin_spi_destroy_queue(drv_data);
  1150. if (status != 0)
  1151. return status;
  1152. /* Disable the SSP at the peripheral and SOC level */
  1153. bfin_spi_disable(drv_data);
  1154. /* Release DMA */
  1155. if (drv_data->master_info->enable_dma) {
  1156. if (dma_channel_active(drv_data->dma_channel))
  1157. free_dma(drv_data->dma_channel);
  1158. }
  1159. if (drv_data->irq_requested) {
  1160. free_irq(drv_data->spi_irq, drv_data);
  1161. drv_data->irq_requested = 0;
  1162. }
  1163. /* Disconnect from the SPI framework */
  1164. spi_unregister_master(drv_data->master);
  1165. peripheral_free_list(drv_data->pin_req);
  1166. return 0;
  1167. }
  1168. #ifdef CONFIG_PM_SLEEP
  1169. static int bfin_spi_suspend(struct device *dev)
  1170. {
  1171. struct bfin_spi_master_data *drv_data = dev_get_drvdata(dev);
  1172. int status = 0;
  1173. status = bfin_spi_stop_queue(drv_data);
  1174. if (status != 0)
  1175. return status;
  1176. drv_data->ctrl_reg = bfin_read(&drv_data->regs->ctl);
  1177. drv_data->flag_reg = bfin_read(&drv_data->regs->flg);
  1178. /*
  1179. * reset SPI_CTL and SPI_FLG registers
  1180. */
  1181. bfin_write(&drv_data->regs->ctl, BIT_CTL_CPHA | BIT_CTL_MASTER);
  1182. bfin_write(&drv_data->regs->flg, 0xFF00);
  1183. return 0;
  1184. }
  1185. static int bfin_spi_resume(struct device *dev)
  1186. {
  1187. struct bfin_spi_master_data *drv_data = dev_get_drvdata(dev);
  1188. int status = 0;
  1189. bfin_write(&drv_data->regs->ctl, drv_data->ctrl_reg);
  1190. bfin_write(&drv_data->regs->flg, drv_data->flag_reg);
  1191. /* Start the queue running */
  1192. status = bfin_spi_start_queue(drv_data);
  1193. if (status != 0) {
  1194. dev_err(dev, "problem starting queue (%d)\n", status);
  1195. return status;
  1196. }
  1197. return 0;
  1198. }
  1199. static SIMPLE_DEV_PM_OPS(bfin_spi_pm_ops, bfin_spi_suspend, bfin_spi_resume);
  1200. #define BFIN_SPI_PM_OPS (&bfin_spi_pm_ops)
  1201. #else
  1202. #define BFIN_SPI_PM_OPS NULL
  1203. #endif
  1204. MODULE_ALIAS("platform:bfin-spi");
  1205. static struct platform_driver bfin_spi_driver = {
  1206. .driver = {
  1207. .name = DRV_NAME,
  1208. .pm = BFIN_SPI_PM_OPS,
  1209. },
  1210. .probe = bfin_spi_probe,
  1211. .remove = bfin_spi_remove,
  1212. };
  1213. static int __init bfin_spi_init(void)
  1214. {
  1215. return platform_driver_register(&bfin_spi_driver);
  1216. }
  1217. subsys_initcall(bfin_spi_init);
  1218. static void __exit bfin_spi_exit(void)
  1219. {
  1220. platform_driver_unregister(&bfin_spi_driver);
  1221. }
  1222. module_exit(bfin_spi_exit);