spi-adi-v3.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /*
  2. * Analog Devices SPI3 controller driver
  3. *
  4. * Copyright (c) 2014 Analog Devices Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/errno.h>
  20. #include <linux/gpio.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/ioport.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/spi/adi_spi3.h>
  30. #include <linux/types.h>
  31. #include <asm/dma.h>
  32. #include <asm/portmux.h>
  33. enum adi_spi_state {
  34. START_STATE,
  35. RUNNING_STATE,
  36. DONE_STATE,
  37. ERROR_STATE
  38. };
  39. struct adi_spi_master;
  40. struct adi_spi_transfer_ops {
  41. void (*write) (struct adi_spi_master *);
  42. void (*read) (struct adi_spi_master *);
  43. void (*duplex) (struct adi_spi_master *);
  44. };
  45. /* runtime info for spi master */
  46. struct adi_spi_master {
  47. /* SPI framework hookup */
  48. struct spi_master *master;
  49. /* Regs base of SPI controller */
  50. struct adi_spi_regs __iomem *regs;
  51. /* Pin request list */
  52. u16 *pin_req;
  53. /* Message Transfer pump */
  54. struct tasklet_struct pump_transfers;
  55. /* Current message transfer state info */
  56. struct spi_message *cur_msg;
  57. struct spi_transfer *cur_transfer;
  58. struct adi_spi_device *cur_chip;
  59. unsigned transfer_len;
  60. /* transfer buffer */
  61. void *tx;
  62. void *tx_end;
  63. void *rx;
  64. void *rx_end;
  65. /* dma info */
  66. unsigned int tx_dma;
  67. unsigned int rx_dma;
  68. dma_addr_t tx_dma_addr;
  69. dma_addr_t rx_dma_addr;
  70. unsigned long dummy_buffer; /* used in unidirectional transfer */
  71. unsigned long tx_dma_size;
  72. unsigned long rx_dma_size;
  73. int tx_num;
  74. int rx_num;
  75. /* store register value for suspend/resume */
  76. u32 control;
  77. u32 ssel;
  78. unsigned long sclk;
  79. enum adi_spi_state state;
  80. const struct adi_spi_transfer_ops *ops;
  81. };
  82. struct adi_spi_device {
  83. u32 control;
  84. u32 clock;
  85. u32 ssel;
  86. u8 cs;
  87. u16 cs_chg_udelay; /* Some devices require > 255usec delay */
  88. u32 cs_gpio;
  89. u32 tx_dummy_val; /* tx value for rx only transfer */
  90. bool enable_dma;
  91. const struct adi_spi_transfer_ops *ops;
  92. };
  93. static void adi_spi_enable(struct adi_spi_master *drv_data)
  94. {
  95. u32 ctl;
  96. ctl = ioread32(&drv_data->regs->control);
  97. ctl |= SPI_CTL_EN;
  98. iowrite32(ctl, &drv_data->regs->control);
  99. }
  100. static void adi_spi_disable(struct adi_spi_master *drv_data)
  101. {
  102. u32 ctl;
  103. ctl = ioread32(&drv_data->regs->control);
  104. ctl &= ~SPI_CTL_EN;
  105. iowrite32(ctl, &drv_data->regs->control);
  106. }
  107. /* Caculate the SPI_CLOCK register value based on input HZ */
  108. static u32 hz_to_spi_clock(u32 sclk, u32 speed_hz)
  109. {
  110. u32 spi_clock = sclk / speed_hz;
  111. if (spi_clock)
  112. spi_clock--;
  113. return spi_clock;
  114. }
  115. static int adi_spi_flush(struct adi_spi_master *drv_data)
  116. {
  117. unsigned long limit = loops_per_jiffy << 1;
  118. /* wait for stop and clear stat */
  119. while (!(ioread32(&drv_data->regs->status) & SPI_STAT_SPIF) && --limit)
  120. cpu_relax();
  121. iowrite32(0xFFFFFFFF, &drv_data->regs->status);
  122. return limit;
  123. }
  124. /* Chip select operation functions for cs_change flag */
  125. static void adi_spi_cs_active(struct adi_spi_master *drv_data, struct adi_spi_device *chip)
  126. {
  127. if (likely(chip->cs < MAX_CTRL_CS)) {
  128. u32 reg;
  129. reg = ioread32(&drv_data->regs->ssel);
  130. reg &= ~chip->ssel;
  131. iowrite32(reg, &drv_data->regs->ssel);
  132. } else {
  133. gpio_set_value(chip->cs_gpio, 0);
  134. }
  135. }
  136. static void adi_spi_cs_deactive(struct adi_spi_master *drv_data,
  137. struct adi_spi_device *chip)
  138. {
  139. if (likely(chip->cs < MAX_CTRL_CS)) {
  140. u32 reg;
  141. reg = ioread32(&drv_data->regs->ssel);
  142. reg |= chip->ssel;
  143. iowrite32(reg, &drv_data->regs->ssel);
  144. } else {
  145. gpio_set_value(chip->cs_gpio, 1);
  146. }
  147. /* Move delay here for consistency */
  148. if (chip->cs_chg_udelay)
  149. udelay(chip->cs_chg_udelay);
  150. }
  151. /* enable or disable the pin muxed by GPIO and SPI CS to work as SPI CS */
  152. static inline void adi_spi_cs_enable(struct adi_spi_master *drv_data,
  153. struct adi_spi_device *chip)
  154. {
  155. if (chip->cs < MAX_CTRL_CS) {
  156. u32 reg;
  157. reg = ioread32(&drv_data->regs->ssel);
  158. reg |= chip->ssel >> 8;
  159. iowrite32(reg, &drv_data->regs->ssel);
  160. }
  161. }
  162. static inline void adi_spi_cs_disable(struct adi_spi_master *drv_data,
  163. struct adi_spi_device *chip)
  164. {
  165. if (chip->cs < MAX_CTRL_CS) {
  166. u32 reg;
  167. reg = ioread32(&drv_data->regs->ssel);
  168. reg &= ~(chip->ssel >> 8);
  169. iowrite32(reg, &drv_data->regs->ssel);
  170. }
  171. }
  172. /* stop controller and re-config current chip*/
  173. static void adi_spi_restore_state(struct adi_spi_master *drv_data)
  174. {
  175. struct adi_spi_device *chip = drv_data->cur_chip;
  176. /* Clear status and disable clock */
  177. iowrite32(0xFFFFFFFF, &drv_data->regs->status);
  178. iowrite32(0x0, &drv_data->regs->rx_control);
  179. iowrite32(0x0, &drv_data->regs->tx_control);
  180. adi_spi_disable(drv_data);
  181. /* Load the registers */
  182. iowrite32(chip->control, &drv_data->regs->control);
  183. iowrite32(chip->clock, &drv_data->regs->clock);
  184. adi_spi_enable(drv_data);
  185. drv_data->tx_num = drv_data->rx_num = 0;
  186. /* we always choose tx transfer initiate */
  187. iowrite32(SPI_RXCTL_REN, &drv_data->regs->rx_control);
  188. iowrite32(SPI_TXCTL_TEN | SPI_TXCTL_TTI, &drv_data->regs->tx_control);
  189. adi_spi_cs_active(drv_data, chip);
  190. }
  191. /* discard invalid rx data and empty rfifo */
  192. static inline void dummy_read(struct adi_spi_master *drv_data)
  193. {
  194. while (!(ioread32(&drv_data->regs->status) & SPI_STAT_RFE))
  195. ioread32(&drv_data->regs->rfifo);
  196. }
  197. static void adi_spi_u8_write(struct adi_spi_master *drv_data)
  198. {
  199. dummy_read(drv_data);
  200. while (drv_data->tx < drv_data->tx_end) {
  201. iowrite32(*(u8 *)(drv_data->tx++), &drv_data->regs->tfifo);
  202. while (ioread32(&drv_data->regs->status) & SPI_STAT_RFE)
  203. cpu_relax();
  204. ioread32(&drv_data->regs->rfifo);
  205. }
  206. }
  207. static void adi_spi_u8_read(struct adi_spi_master *drv_data)
  208. {
  209. u32 tx_val = drv_data->cur_chip->tx_dummy_val;
  210. dummy_read(drv_data);
  211. while (drv_data->rx < drv_data->rx_end) {
  212. iowrite32(tx_val, &drv_data->regs->tfifo);
  213. while (ioread32(&drv_data->regs->status) & SPI_STAT_RFE)
  214. cpu_relax();
  215. *(u8 *)(drv_data->rx++) = ioread32(&drv_data->regs->rfifo);
  216. }
  217. }
  218. static void adi_spi_u8_duplex(struct adi_spi_master *drv_data)
  219. {
  220. dummy_read(drv_data);
  221. while (drv_data->rx < drv_data->rx_end) {
  222. iowrite32(*(u8 *)(drv_data->tx++), &drv_data->regs->tfifo);
  223. while (ioread32(&drv_data->regs->status) & SPI_STAT_RFE)
  224. cpu_relax();
  225. *(u8 *)(drv_data->rx++) = ioread32(&drv_data->regs->rfifo);
  226. }
  227. }
  228. static const struct adi_spi_transfer_ops adi_spi_transfer_ops_u8 = {
  229. .write = adi_spi_u8_write,
  230. .read = adi_spi_u8_read,
  231. .duplex = adi_spi_u8_duplex,
  232. };
  233. static void adi_spi_u16_write(struct adi_spi_master *drv_data)
  234. {
  235. dummy_read(drv_data);
  236. while (drv_data->tx < drv_data->tx_end) {
  237. iowrite32(*(u16 *)drv_data->tx, &drv_data->regs->tfifo);
  238. drv_data->tx += 2;
  239. while (ioread32(&drv_data->regs->status) & SPI_STAT_RFE)
  240. cpu_relax();
  241. ioread32(&drv_data->regs->rfifo);
  242. }
  243. }
  244. static void adi_spi_u16_read(struct adi_spi_master *drv_data)
  245. {
  246. u32 tx_val = drv_data->cur_chip->tx_dummy_val;
  247. dummy_read(drv_data);
  248. while (drv_data->rx < drv_data->rx_end) {
  249. iowrite32(tx_val, &drv_data->regs->tfifo);
  250. while (ioread32(&drv_data->regs->status) & SPI_STAT_RFE)
  251. cpu_relax();
  252. *(u16 *)drv_data->rx = ioread32(&drv_data->regs->rfifo);
  253. drv_data->rx += 2;
  254. }
  255. }
  256. static void adi_spi_u16_duplex(struct adi_spi_master *drv_data)
  257. {
  258. dummy_read(drv_data);
  259. while (drv_data->rx < drv_data->rx_end) {
  260. iowrite32(*(u16 *)drv_data->tx, &drv_data->regs->tfifo);
  261. drv_data->tx += 2;
  262. while (ioread32(&drv_data->regs->status) & SPI_STAT_RFE)
  263. cpu_relax();
  264. *(u16 *)drv_data->rx = ioread32(&drv_data->regs->rfifo);
  265. drv_data->rx += 2;
  266. }
  267. }
  268. static const struct adi_spi_transfer_ops adi_spi_transfer_ops_u16 = {
  269. .write = adi_spi_u16_write,
  270. .read = adi_spi_u16_read,
  271. .duplex = adi_spi_u16_duplex,
  272. };
  273. static void adi_spi_u32_write(struct adi_spi_master *drv_data)
  274. {
  275. dummy_read(drv_data);
  276. while (drv_data->tx < drv_data->tx_end) {
  277. iowrite32(*(u32 *)drv_data->tx, &drv_data->regs->tfifo);
  278. drv_data->tx += 4;
  279. while (ioread32(&drv_data->regs->status) & SPI_STAT_RFE)
  280. cpu_relax();
  281. ioread32(&drv_data->regs->rfifo);
  282. }
  283. }
  284. static void adi_spi_u32_read(struct adi_spi_master *drv_data)
  285. {
  286. u32 tx_val = drv_data->cur_chip->tx_dummy_val;
  287. dummy_read(drv_data);
  288. while (drv_data->rx < drv_data->rx_end) {
  289. iowrite32(tx_val, &drv_data->regs->tfifo);
  290. while (ioread32(&drv_data->regs->status) & SPI_STAT_RFE)
  291. cpu_relax();
  292. *(u32 *)drv_data->rx = ioread32(&drv_data->regs->rfifo);
  293. drv_data->rx += 4;
  294. }
  295. }
  296. static void adi_spi_u32_duplex(struct adi_spi_master *drv_data)
  297. {
  298. dummy_read(drv_data);
  299. while (drv_data->rx < drv_data->rx_end) {
  300. iowrite32(*(u32 *)drv_data->tx, &drv_data->regs->tfifo);
  301. drv_data->tx += 4;
  302. while (ioread32(&drv_data->regs->status) & SPI_STAT_RFE)
  303. cpu_relax();
  304. *(u32 *)drv_data->rx = ioread32(&drv_data->regs->rfifo);
  305. drv_data->rx += 4;
  306. }
  307. }
  308. static const struct adi_spi_transfer_ops adi_spi_transfer_ops_u32 = {
  309. .write = adi_spi_u32_write,
  310. .read = adi_spi_u32_read,
  311. .duplex = adi_spi_u32_duplex,
  312. };
  313. /* test if there is more transfer to be done */
  314. static void adi_spi_next_transfer(struct adi_spi_master *drv)
  315. {
  316. struct spi_message *msg = drv->cur_msg;
  317. struct spi_transfer *t = drv->cur_transfer;
  318. /* Move to next transfer */
  319. if (t->transfer_list.next != &msg->transfers) {
  320. drv->cur_transfer = list_entry(t->transfer_list.next,
  321. struct spi_transfer, transfer_list);
  322. drv->state = RUNNING_STATE;
  323. } else {
  324. drv->state = DONE_STATE;
  325. drv->cur_transfer = NULL;
  326. }
  327. }
  328. static void adi_spi_giveback(struct adi_spi_master *drv_data)
  329. {
  330. struct adi_spi_device *chip = drv_data->cur_chip;
  331. adi_spi_cs_deactive(drv_data, chip);
  332. spi_finalize_current_message(drv_data->master);
  333. }
  334. static int adi_spi_setup_transfer(struct adi_spi_master *drv)
  335. {
  336. struct spi_transfer *t = drv->cur_transfer;
  337. u32 cr, cr_width;
  338. if (t->tx_buf) {
  339. drv->tx = (void *)t->tx_buf;
  340. drv->tx_end = drv->tx + t->len;
  341. } else {
  342. drv->tx = NULL;
  343. }
  344. if (t->rx_buf) {
  345. drv->rx = t->rx_buf;
  346. drv->rx_end = drv->rx + t->len;
  347. } else {
  348. drv->rx = NULL;
  349. }
  350. drv->transfer_len = t->len;
  351. /* bits per word setup */
  352. switch (t->bits_per_word) {
  353. case 8:
  354. cr_width = SPI_CTL_SIZE08;
  355. drv->ops = &adi_spi_transfer_ops_u8;
  356. break;
  357. case 16:
  358. cr_width = SPI_CTL_SIZE16;
  359. drv->ops = &adi_spi_transfer_ops_u16;
  360. break;
  361. case 32:
  362. cr_width = SPI_CTL_SIZE32;
  363. drv->ops = &adi_spi_transfer_ops_u32;
  364. break;
  365. default:
  366. return -EINVAL;
  367. }
  368. cr = ioread32(&drv->regs->control) & ~SPI_CTL_SIZE;
  369. cr |= cr_width;
  370. iowrite32(cr, &drv->regs->control);
  371. /* speed setup */
  372. iowrite32(hz_to_spi_clock(drv->sclk, t->speed_hz), &drv->regs->clock);
  373. return 0;
  374. }
  375. static int adi_spi_dma_xfer(struct adi_spi_master *drv_data)
  376. {
  377. struct spi_transfer *t = drv_data->cur_transfer;
  378. struct spi_message *msg = drv_data->cur_msg;
  379. struct adi_spi_device *chip = drv_data->cur_chip;
  380. u32 dma_config;
  381. unsigned long word_count, word_size;
  382. void *tx_buf, *rx_buf;
  383. switch (t->bits_per_word) {
  384. case 8:
  385. dma_config = WDSIZE_8 | PSIZE_8;
  386. word_count = drv_data->transfer_len;
  387. word_size = 1;
  388. break;
  389. case 16:
  390. dma_config = WDSIZE_16 | PSIZE_16;
  391. word_count = drv_data->transfer_len / 2;
  392. word_size = 2;
  393. break;
  394. default:
  395. dma_config = WDSIZE_32 | PSIZE_32;
  396. word_count = drv_data->transfer_len / 4;
  397. word_size = 4;
  398. break;
  399. }
  400. if (!drv_data->rx) {
  401. tx_buf = drv_data->tx;
  402. rx_buf = &drv_data->dummy_buffer;
  403. drv_data->tx_dma_size = drv_data->transfer_len;
  404. drv_data->rx_dma_size = sizeof(drv_data->dummy_buffer);
  405. set_dma_x_modify(drv_data->tx_dma, word_size);
  406. set_dma_x_modify(drv_data->rx_dma, 0);
  407. } else if (!drv_data->tx) {
  408. drv_data->dummy_buffer = chip->tx_dummy_val;
  409. tx_buf = &drv_data->dummy_buffer;
  410. rx_buf = drv_data->rx;
  411. drv_data->tx_dma_size = sizeof(drv_data->dummy_buffer);
  412. drv_data->rx_dma_size = drv_data->transfer_len;
  413. set_dma_x_modify(drv_data->tx_dma, 0);
  414. set_dma_x_modify(drv_data->rx_dma, word_size);
  415. } else {
  416. tx_buf = drv_data->tx;
  417. rx_buf = drv_data->rx;
  418. drv_data->tx_dma_size = drv_data->rx_dma_size
  419. = drv_data->transfer_len;
  420. set_dma_x_modify(drv_data->tx_dma, word_size);
  421. set_dma_x_modify(drv_data->rx_dma, word_size);
  422. }
  423. drv_data->tx_dma_addr = dma_map_single(&msg->spi->dev,
  424. (void *)tx_buf,
  425. drv_data->tx_dma_size,
  426. DMA_TO_DEVICE);
  427. if (dma_mapping_error(&msg->spi->dev,
  428. drv_data->tx_dma_addr))
  429. return -ENOMEM;
  430. drv_data->rx_dma_addr = dma_map_single(&msg->spi->dev,
  431. (void *)rx_buf,
  432. drv_data->rx_dma_size,
  433. DMA_FROM_DEVICE);
  434. if (dma_mapping_error(&msg->spi->dev,
  435. drv_data->rx_dma_addr)) {
  436. dma_unmap_single(&msg->spi->dev,
  437. drv_data->tx_dma_addr,
  438. drv_data->tx_dma_size,
  439. DMA_TO_DEVICE);
  440. return -ENOMEM;
  441. }
  442. dummy_read(drv_data);
  443. set_dma_x_count(drv_data->tx_dma, word_count);
  444. set_dma_x_count(drv_data->rx_dma, word_count);
  445. set_dma_start_addr(drv_data->tx_dma, drv_data->tx_dma_addr);
  446. set_dma_start_addr(drv_data->rx_dma, drv_data->rx_dma_addr);
  447. dma_config |= DMAFLOW_STOP | RESTART | DI_EN;
  448. set_dma_config(drv_data->tx_dma, dma_config);
  449. set_dma_config(drv_data->rx_dma, dma_config | WNR);
  450. enable_dma(drv_data->tx_dma);
  451. enable_dma(drv_data->rx_dma);
  452. iowrite32(SPI_RXCTL_REN | SPI_RXCTL_RDR_NE,
  453. &drv_data->regs->rx_control);
  454. iowrite32(SPI_TXCTL_TEN | SPI_TXCTL_TTI | SPI_TXCTL_TDR_NF,
  455. &drv_data->regs->tx_control);
  456. return 0;
  457. }
  458. static int adi_spi_pio_xfer(struct adi_spi_master *drv_data)
  459. {
  460. struct spi_message *msg = drv_data->cur_msg;
  461. if (!drv_data->rx) {
  462. /* write only half duplex */
  463. drv_data->ops->write(drv_data);
  464. if (drv_data->tx != drv_data->tx_end)
  465. return -EIO;
  466. } else if (!drv_data->tx) {
  467. /* read only half duplex */
  468. drv_data->ops->read(drv_data);
  469. if (drv_data->rx != drv_data->rx_end)
  470. return -EIO;
  471. } else {
  472. /* full duplex mode */
  473. drv_data->ops->duplex(drv_data);
  474. if (drv_data->tx != drv_data->tx_end)
  475. return -EIO;
  476. }
  477. if (!adi_spi_flush(drv_data))
  478. return -EIO;
  479. msg->actual_length += drv_data->transfer_len;
  480. tasklet_schedule(&drv_data->pump_transfers);
  481. return 0;
  482. }
  483. static void adi_spi_pump_transfers(unsigned long data)
  484. {
  485. struct adi_spi_master *drv_data = (struct adi_spi_master *)data;
  486. struct spi_message *msg = NULL;
  487. struct spi_transfer *t = NULL;
  488. struct adi_spi_device *chip = NULL;
  489. int ret;
  490. /* Get current state information */
  491. msg = drv_data->cur_msg;
  492. t = drv_data->cur_transfer;
  493. chip = drv_data->cur_chip;
  494. /* Handle for abort */
  495. if (drv_data->state == ERROR_STATE) {
  496. msg->status = -EIO;
  497. adi_spi_giveback(drv_data);
  498. return;
  499. }
  500. if (drv_data->state == RUNNING_STATE) {
  501. if (t->delay_usecs)
  502. udelay(t->delay_usecs);
  503. if (t->cs_change)
  504. adi_spi_cs_deactive(drv_data, chip);
  505. adi_spi_next_transfer(drv_data);
  506. t = drv_data->cur_transfer;
  507. }
  508. /* Handle end of message */
  509. if (drv_data->state == DONE_STATE) {
  510. msg->status = 0;
  511. adi_spi_giveback(drv_data);
  512. return;
  513. }
  514. if ((t->len == 0) || (t->tx_buf == NULL && t->rx_buf == NULL)) {
  515. /* Schedule next transfer tasklet */
  516. tasklet_schedule(&drv_data->pump_transfers);
  517. return;
  518. }
  519. ret = adi_spi_setup_transfer(drv_data);
  520. if (ret) {
  521. msg->status = ret;
  522. adi_spi_giveback(drv_data);
  523. }
  524. iowrite32(0xFFFFFFFF, &drv_data->regs->status);
  525. adi_spi_cs_active(drv_data, chip);
  526. drv_data->state = RUNNING_STATE;
  527. if (chip->enable_dma)
  528. ret = adi_spi_dma_xfer(drv_data);
  529. else
  530. ret = adi_spi_pio_xfer(drv_data);
  531. if (ret) {
  532. msg->status = ret;
  533. adi_spi_giveback(drv_data);
  534. }
  535. }
  536. static int adi_spi_transfer_one_message(struct spi_master *master,
  537. struct spi_message *m)
  538. {
  539. struct adi_spi_master *drv_data = spi_master_get_devdata(master);
  540. drv_data->cur_msg = m;
  541. drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
  542. adi_spi_restore_state(drv_data);
  543. drv_data->state = START_STATE;
  544. drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
  545. struct spi_transfer, transfer_list);
  546. tasklet_schedule(&drv_data->pump_transfers);
  547. return 0;
  548. }
  549. #define MAX_SPI_SSEL 7
  550. static const u16 ssel[][MAX_SPI_SSEL] = {
  551. {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
  552. P_SPI0_SSEL4, P_SPI0_SSEL5,
  553. P_SPI0_SSEL6, P_SPI0_SSEL7},
  554. {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
  555. P_SPI1_SSEL4, P_SPI1_SSEL5,
  556. P_SPI1_SSEL6, P_SPI1_SSEL7},
  557. {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
  558. P_SPI2_SSEL4, P_SPI2_SSEL5,
  559. P_SPI2_SSEL6, P_SPI2_SSEL7},
  560. };
  561. static int adi_spi_setup(struct spi_device *spi)
  562. {
  563. struct adi_spi_master *drv_data = spi_master_get_devdata(spi->master);
  564. struct adi_spi_device *chip = spi_get_ctldata(spi);
  565. u32 ctl_reg = SPI_CTL_ODM | SPI_CTL_PSSE;
  566. int ret = -EINVAL;
  567. if (!chip) {
  568. struct adi_spi3_chip *chip_info = spi->controller_data;
  569. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  570. if (!chip)
  571. return -ENOMEM;
  572. if (chip_info) {
  573. if (chip_info->control & ~ctl_reg) {
  574. dev_err(&spi->dev,
  575. "do not set bits that the SPI framework manages\n");
  576. goto error;
  577. }
  578. chip->control = chip_info->control;
  579. chip->cs_chg_udelay = chip_info->cs_chg_udelay;
  580. chip->tx_dummy_val = chip_info->tx_dummy_val;
  581. chip->enable_dma = chip_info->enable_dma;
  582. }
  583. chip->cs = spi->chip_select;
  584. if (chip->cs < MAX_CTRL_CS) {
  585. chip->ssel = (1 << chip->cs) << 8;
  586. ret = peripheral_request(ssel[spi->master->bus_num]
  587. [chip->cs-1], dev_name(&spi->dev));
  588. if (ret) {
  589. dev_err(&spi->dev, "peripheral_request() error\n");
  590. goto error;
  591. }
  592. } else {
  593. chip->cs_gpio = chip->cs - MAX_CTRL_CS;
  594. ret = gpio_request_one(chip->cs_gpio, GPIOF_OUT_INIT_HIGH,
  595. dev_name(&spi->dev));
  596. if (ret) {
  597. dev_err(&spi->dev, "gpio_request_one() error\n");
  598. goto error;
  599. }
  600. }
  601. spi_set_ctldata(spi, chip);
  602. }
  603. /* force a default base state */
  604. chip->control &= ctl_reg;
  605. if (spi->mode & SPI_CPOL)
  606. chip->control |= SPI_CTL_CPOL;
  607. if (spi->mode & SPI_CPHA)
  608. chip->control |= SPI_CTL_CPHA;
  609. if (spi->mode & SPI_LSB_FIRST)
  610. chip->control |= SPI_CTL_LSBF;
  611. chip->control |= SPI_CTL_MSTR;
  612. /* we choose software to controll cs */
  613. chip->control &= ~SPI_CTL_ASSEL;
  614. chip->clock = hz_to_spi_clock(drv_data->sclk, spi->max_speed_hz);
  615. adi_spi_cs_enable(drv_data, chip);
  616. adi_spi_cs_deactive(drv_data, chip);
  617. return 0;
  618. error:
  619. if (chip) {
  620. kfree(chip);
  621. spi_set_ctldata(spi, NULL);
  622. }
  623. return ret;
  624. }
  625. static void adi_spi_cleanup(struct spi_device *spi)
  626. {
  627. struct adi_spi_device *chip = spi_get_ctldata(spi);
  628. struct adi_spi_master *drv_data = spi_master_get_devdata(spi->master);
  629. if (!chip)
  630. return;
  631. if (chip->cs < MAX_CTRL_CS) {
  632. peripheral_free(ssel[spi->master->bus_num]
  633. [chip->cs-1]);
  634. adi_spi_cs_disable(drv_data, chip);
  635. } else {
  636. gpio_free(chip->cs_gpio);
  637. }
  638. kfree(chip);
  639. spi_set_ctldata(spi, NULL);
  640. }
  641. static irqreturn_t adi_spi_tx_dma_isr(int irq, void *dev_id)
  642. {
  643. struct adi_spi_master *drv_data = dev_id;
  644. u32 dma_stat = get_dma_curr_irqstat(drv_data->tx_dma);
  645. u32 tx_ctl;
  646. clear_dma_irqstat(drv_data->tx_dma);
  647. if (dma_stat & DMA_DONE) {
  648. drv_data->tx_num++;
  649. } else {
  650. dev_err(&drv_data->master->dev,
  651. "spi tx dma error: %d\n", dma_stat);
  652. if (drv_data->tx)
  653. drv_data->state = ERROR_STATE;
  654. }
  655. tx_ctl = ioread32(&drv_data->regs->tx_control);
  656. tx_ctl &= ~SPI_TXCTL_TDR_NF;
  657. iowrite32(tx_ctl, &drv_data->regs->tx_control);
  658. return IRQ_HANDLED;
  659. }
  660. static irqreturn_t adi_spi_rx_dma_isr(int irq, void *dev_id)
  661. {
  662. struct adi_spi_master *drv_data = dev_id;
  663. struct spi_message *msg = drv_data->cur_msg;
  664. u32 dma_stat = get_dma_curr_irqstat(drv_data->rx_dma);
  665. clear_dma_irqstat(drv_data->rx_dma);
  666. if (dma_stat & DMA_DONE) {
  667. drv_data->rx_num++;
  668. /* we may fail on tx dma */
  669. if (drv_data->state != ERROR_STATE)
  670. msg->actual_length += drv_data->transfer_len;
  671. } else {
  672. drv_data->state = ERROR_STATE;
  673. dev_err(&drv_data->master->dev,
  674. "spi rx dma error: %d\n", dma_stat);
  675. }
  676. iowrite32(0, &drv_data->regs->tx_control);
  677. iowrite32(0, &drv_data->regs->rx_control);
  678. if (drv_data->rx_num != drv_data->tx_num)
  679. dev_dbg(&drv_data->master->dev,
  680. "dma interrupt missing: tx=%d,rx=%d\n",
  681. drv_data->tx_num, drv_data->rx_num);
  682. tasklet_schedule(&drv_data->pump_transfers);
  683. return IRQ_HANDLED;
  684. }
  685. static int adi_spi_probe(struct platform_device *pdev)
  686. {
  687. struct device *dev = &pdev->dev;
  688. struct adi_spi3_master *info = dev_get_platdata(dev);
  689. struct spi_master *master;
  690. struct adi_spi_master *drv_data;
  691. struct resource *mem, *res;
  692. unsigned int tx_dma, rx_dma;
  693. struct clk *sclk;
  694. int ret;
  695. if (!info) {
  696. dev_err(dev, "platform data missing!\n");
  697. return -ENODEV;
  698. }
  699. sclk = devm_clk_get(dev, "spi");
  700. if (IS_ERR(sclk)) {
  701. dev_err(dev, "can not get spi clock\n");
  702. return PTR_ERR(sclk);
  703. }
  704. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  705. if (!res) {
  706. dev_err(dev, "can not get tx dma resource\n");
  707. return -ENXIO;
  708. }
  709. tx_dma = res->start;
  710. res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  711. if (!res) {
  712. dev_err(dev, "can not get rx dma resource\n");
  713. return -ENXIO;
  714. }
  715. rx_dma = res->start;
  716. /* allocate master with space for drv_data */
  717. master = spi_alloc_master(dev, sizeof(*drv_data));
  718. if (!master) {
  719. dev_err(dev, "can not alloc spi_master\n");
  720. return -ENOMEM;
  721. }
  722. platform_set_drvdata(pdev, master);
  723. /* the mode bits supported by this driver */
  724. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
  725. master->bus_num = pdev->id;
  726. master->num_chipselect = info->num_chipselect;
  727. master->cleanup = adi_spi_cleanup;
  728. master->setup = adi_spi_setup;
  729. master->transfer_one_message = adi_spi_transfer_one_message;
  730. master->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(16) |
  731. SPI_BPW_MASK(8);
  732. drv_data = spi_master_get_devdata(master);
  733. drv_data->master = master;
  734. drv_data->tx_dma = tx_dma;
  735. drv_data->rx_dma = rx_dma;
  736. drv_data->pin_req = info->pin_req;
  737. drv_data->sclk = clk_get_rate(sclk);
  738. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  739. drv_data->regs = devm_ioremap_resource(dev, mem);
  740. if (IS_ERR(drv_data->regs)) {
  741. ret = PTR_ERR(drv_data->regs);
  742. goto err_put_master;
  743. }
  744. /* request tx and rx dma */
  745. ret = request_dma(tx_dma, "SPI_TX_DMA");
  746. if (ret) {
  747. dev_err(dev, "can not request SPI TX DMA channel\n");
  748. goto err_put_master;
  749. }
  750. set_dma_callback(tx_dma, adi_spi_tx_dma_isr, drv_data);
  751. ret = request_dma(rx_dma, "SPI_RX_DMA");
  752. if (ret) {
  753. dev_err(dev, "can not request SPI RX DMA channel\n");
  754. goto err_free_tx_dma;
  755. }
  756. set_dma_callback(drv_data->rx_dma, adi_spi_rx_dma_isr, drv_data);
  757. /* request CLK, MOSI and MISO */
  758. ret = peripheral_request_list(drv_data->pin_req, "adi-spi3");
  759. if (ret < 0) {
  760. dev_err(dev, "can not request spi pins\n");
  761. goto err_free_rx_dma;
  762. }
  763. iowrite32(SPI_CTL_MSTR | SPI_CTL_CPHA, &drv_data->regs->control);
  764. iowrite32(0x0000FE00, &drv_data->regs->ssel);
  765. iowrite32(0x0, &drv_data->regs->delay);
  766. tasklet_init(&drv_data->pump_transfers,
  767. adi_spi_pump_transfers, (unsigned long)drv_data);
  768. /* register with the SPI framework */
  769. ret = devm_spi_register_master(dev, master);
  770. if (ret) {
  771. dev_err(dev, "can not register spi master\n");
  772. goto err_free_peripheral;
  773. }
  774. return ret;
  775. err_free_peripheral:
  776. peripheral_free_list(drv_data->pin_req);
  777. err_free_rx_dma:
  778. free_dma(rx_dma);
  779. err_free_tx_dma:
  780. free_dma(tx_dma);
  781. err_put_master:
  782. spi_master_put(master);
  783. return ret;
  784. }
  785. static int adi_spi_remove(struct platform_device *pdev)
  786. {
  787. struct spi_master *master = platform_get_drvdata(pdev);
  788. struct adi_spi_master *drv_data = spi_master_get_devdata(master);
  789. adi_spi_disable(drv_data);
  790. peripheral_free_list(drv_data->pin_req);
  791. free_dma(drv_data->rx_dma);
  792. free_dma(drv_data->tx_dma);
  793. return 0;
  794. }
  795. #ifdef CONFIG_PM
  796. static int adi_spi_suspend(struct device *dev)
  797. {
  798. struct spi_master *master = dev_get_drvdata(dev);
  799. struct adi_spi_master *drv_data = spi_master_get_devdata(master);
  800. spi_master_suspend(master);
  801. drv_data->control = ioread32(&drv_data->regs->control);
  802. drv_data->ssel = ioread32(&drv_data->regs->ssel);
  803. iowrite32(SPI_CTL_MSTR | SPI_CTL_CPHA, &drv_data->regs->control);
  804. iowrite32(0x0000FE00, &drv_data->regs->ssel);
  805. dma_disable_irq(drv_data->rx_dma);
  806. dma_disable_irq(drv_data->tx_dma);
  807. return 0;
  808. }
  809. static int adi_spi_resume(struct device *dev)
  810. {
  811. struct spi_master *master = dev_get_drvdata(dev);
  812. struct adi_spi_master *drv_data = spi_master_get_devdata(master);
  813. int ret = 0;
  814. /* bootrom may modify spi and dma status when resume in spi boot mode */
  815. disable_dma(drv_data->rx_dma);
  816. dma_enable_irq(drv_data->rx_dma);
  817. dma_enable_irq(drv_data->tx_dma);
  818. iowrite32(drv_data->control, &drv_data->regs->control);
  819. iowrite32(drv_data->ssel, &drv_data->regs->ssel);
  820. ret = spi_master_resume(master);
  821. if (ret) {
  822. free_dma(drv_data->rx_dma);
  823. free_dma(drv_data->tx_dma);
  824. }
  825. return ret;
  826. }
  827. #endif
  828. static const struct dev_pm_ops adi_spi_pm_ops = {
  829. SET_SYSTEM_SLEEP_PM_OPS(adi_spi_suspend, adi_spi_resume)
  830. };
  831. MODULE_ALIAS("platform:adi-spi3");
  832. static struct platform_driver adi_spi_driver = {
  833. .driver = {
  834. .name = "adi-spi3",
  835. .pm = &adi_spi_pm_ops,
  836. },
  837. .remove = adi_spi_remove,
  838. };
  839. module_platform_driver_probe(adi_spi_driver, adi_spi_probe);
  840. MODULE_DESCRIPTION("Analog Devices SPI3 controller driver");
  841. MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
  842. MODULE_LICENSE("GPL v2");