spi-bfin5xx.c 39 KB

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