spi_bfin5xx.c 40 KB

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