spi-davinci.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /*
  2. * Copyright (C) 2009 Texas Instruments.
  3. * Copyright (C) 2010 EF Johnson Technologies
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  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/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/gpio.h>
  18. #include <linux/module.h>
  19. #include <linux/delay.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/err.h>
  22. #include <linux/clk.h>
  23. #include <linux/dmaengine.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/spi/spi_bitbang.h>
  30. #include <linux/slab.h>
  31. #include <linux/platform_data/spi-davinci.h>
  32. #define CS_DEFAULT 0xFF
  33. #define SPIFMT_PHASE_MASK BIT(16)
  34. #define SPIFMT_POLARITY_MASK BIT(17)
  35. #define SPIFMT_DISTIMER_MASK BIT(18)
  36. #define SPIFMT_SHIFTDIR_MASK BIT(20)
  37. #define SPIFMT_WAITENA_MASK BIT(21)
  38. #define SPIFMT_PARITYENA_MASK BIT(22)
  39. #define SPIFMT_ODD_PARITY_MASK BIT(23)
  40. #define SPIFMT_WDELAY_MASK 0x3f000000u
  41. #define SPIFMT_WDELAY_SHIFT 24
  42. #define SPIFMT_PRESCALE_SHIFT 8
  43. /* SPIPC0 */
  44. #define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
  45. #define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
  46. #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
  47. #define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
  48. #define SPIINT_MASKALL 0x0101035F
  49. #define SPIINT_MASKINT 0x0000015F
  50. #define SPI_INTLVL_1 0x000001FF
  51. #define SPI_INTLVL_0 0x00000000
  52. /* SPIDAT1 (upper 16 bit defines) */
  53. #define SPIDAT1_CSHOLD_MASK BIT(12)
  54. #define SPIDAT1_WDEL BIT(10)
  55. /* SPIGCR1 */
  56. #define SPIGCR1_CLKMOD_MASK BIT(1)
  57. #define SPIGCR1_MASTER_MASK BIT(0)
  58. #define SPIGCR1_POWERDOWN_MASK BIT(8)
  59. #define SPIGCR1_LOOPBACK_MASK BIT(16)
  60. #define SPIGCR1_SPIENA_MASK BIT(24)
  61. /* SPIBUF */
  62. #define SPIBUF_TXFULL_MASK BIT(29)
  63. #define SPIBUF_RXEMPTY_MASK BIT(31)
  64. /* SPIDELAY */
  65. #define SPIDELAY_C2TDELAY_SHIFT 24
  66. #define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
  67. #define SPIDELAY_T2CDELAY_SHIFT 16
  68. #define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
  69. #define SPIDELAY_T2EDELAY_SHIFT 8
  70. #define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
  71. #define SPIDELAY_C2EDELAY_SHIFT 0
  72. #define SPIDELAY_C2EDELAY_MASK 0xFF
  73. /* Error Masks */
  74. #define SPIFLG_DLEN_ERR_MASK BIT(0)
  75. #define SPIFLG_TIMEOUT_MASK BIT(1)
  76. #define SPIFLG_PARERR_MASK BIT(2)
  77. #define SPIFLG_DESYNC_MASK BIT(3)
  78. #define SPIFLG_BITERR_MASK BIT(4)
  79. #define SPIFLG_OVRRUN_MASK BIT(6)
  80. #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
  81. #define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \
  82. | SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
  83. | SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
  84. | SPIFLG_OVRRUN_MASK)
  85. #define SPIINT_DMA_REQ_EN BIT(16)
  86. /* SPI Controller registers */
  87. #define SPIGCR0 0x00
  88. #define SPIGCR1 0x04
  89. #define SPIINT 0x08
  90. #define SPILVL 0x0c
  91. #define SPIFLG 0x10
  92. #define SPIPC0 0x14
  93. #define SPIDAT1 0x3c
  94. #define SPIBUF 0x40
  95. #define SPIDELAY 0x48
  96. #define SPIDEF 0x4c
  97. #define SPIFMT0 0x50
  98. /* SPI Controller driver's private data. */
  99. struct davinci_spi {
  100. struct spi_bitbang bitbang;
  101. struct clk *clk;
  102. u8 version;
  103. resource_size_t pbase;
  104. void __iomem *base;
  105. u32 irq;
  106. struct completion done;
  107. const void *tx;
  108. void *rx;
  109. int rcount;
  110. int wcount;
  111. struct dma_chan *dma_rx;
  112. struct dma_chan *dma_tx;
  113. struct davinci_spi_platform_data pdata;
  114. void (*get_rx)(u32 rx_data, struct davinci_spi *);
  115. u32 (*get_tx)(struct davinci_spi *);
  116. u8 *bytes_per_word;
  117. u8 prescaler_limit;
  118. };
  119. static struct davinci_spi_config davinci_spi_default_cfg;
  120. static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *dspi)
  121. {
  122. if (dspi->rx) {
  123. u8 *rx = dspi->rx;
  124. *rx++ = (u8)data;
  125. dspi->rx = rx;
  126. }
  127. }
  128. static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *dspi)
  129. {
  130. if (dspi->rx) {
  131. u16 *rx = dspi->rx;
  132. *rx++ = (u16)data;
  133. dspi->rx = rx;
  134. }
  135. }
  136. static u32 davinci_spi_tx_buf_u8(struct davinci_spi *dspi)
  137. {
  138. u32 data = 0;
  139. if (dspi->tx) {
  140. const u8 *tx = dspi->tx;
  141. data = *tx++;
  142. dspi->tx = tx;
  143. }
  144. return data;
  145. }
  146. static u32 davinci_spi_tx_buf_u16(struct davinci_spi *dspi)
  147. {
  148. u32 data = 0;
  149. if (dspi->tx) {
  150. const u16 *tx = dspi->tx;
  151. data = *tx++;
  152. dspi->tx = tx;
  153. }
  154. return data;
  155. }
  156. static inline void set_io_bits(void __iomem *addr, u32 bits)
  157. {
  158. u32 v = ioread32(addr);
  159. v |= bits;
  160. iowrite32(v, addr);
  161. }
  162. static inline void clear_io_bits(void __iomem *addr, u32 bits)
  163. {
  164. u32 v = ioread32(addr);
  165. v &= ~bits;
  166. iowrite32(v, addr);
  167. }
  168. /*
  169. * Interface to control the chip select signal
  170. */
  171. static void davinci_spi_chipselect(struct spi_device *spi, int value)
  172. {
  173. struct davinci_spi *dspi;
  174. struct davinci_spi_platform_data *pdata;
  175. struct davinci_spi_config *spicfg = spi->controller_data;
  176. u8 chip_sel = spi->chip_select;
  177. u16 spidat1 = CS_DEFAULT;
  178. dspi = spi_master_get_devdata(spi->master);
  179. pdata = &dspi->pdata;
  180. /* program delay transfers if tx_delay is non zero */
  181. if (spicfg->wdelay)
  182. spidat1 |= SPIDAT1_WDEL;
  183. /*
  184. * Board specific chip select logic decides the polarity and cs
  185. * line for the controller
  186. */
  187. if (spi->cs_gpio >= 0) {
  188. if (value == BITBANG_CS_ACTIVE)
  189. gpio_set_value(spi->cs_gpio, spi->mode & SPI_CS_HIGH);
  190. else
  191. gpio_set_value(spi->cs_gpio,
  192. !(spi->mode & SPI_CS_HIGH));
  193. } else {
  194. if (value == BITBANG_CS_ACTIVE) {
  195. spidat1 |= SPIDAT1_CSHOLD_MASK;
  196. spidat1 &= ~(0x1 << chip_sel);
  197. }
  198. }
  199. iowrite16(spidat1, dspi->base + SPIDAT1 + 2);
  200. }
  201. /**
  202. * davinci_spi_get_prescale - Calculates the correct prescale value
  203. * @maxspeed_hz: the maximum rate the SPI clock can run at
  204. *
  205. * This function calculates the prescale value that generates a clock rate
  206. * less than or equal to the specified maximum.
  207. *
  208. * Returns: calculated prescale value for easy programming into SPI registers
  209. * or negative error number if valid prescalar cannot be updated.
  210. */
  211. static inline int davinci_spi_get_prescale(struct davinci_spi *dspi,
  212. u32 max_speed_hz)
  213. {
  214. int ret;
  215. /* Subtract 1 to match what will be programmed into SPI register. */
  216. ret = DIV_ROUND_UP(clk_get_rate(dspi->clk), max_speed_hz) - 1;
  217. if (ret < dspi->prescaler_limit || ret > 255)
  218. return -EINVAL;
  219. return ret;
  220. }
  221. /**
  222. * davinci_spi_setup_transfer - This functions will determine transfer method
  223. * @spi: spi device on which data transfer to be done
  224. * @t: spi transfer in which transfer info is filled
  225. *
  226. * This function determines data transfer method (8/16/32 bit transfer).
  227. * It will also set the SPI Clock Control register according to
  228. * SPI slave device freq.
  229. */
  230. static int davinci_spi_setup_transfer(struct spi_device *spi,
  231. struct spi_transfer *t)
  232. {
  233. struct davinci_spi *dspi;
  234. struct davinci_spi_config *spicfg;
  235. u8 bits_per_word = 0;
  236. u32 hz = 0, spifmt = 0;
  237. int prescale;
  238. dspi = spi_master_get_devdata(spi->master);
  239. spicfg = spi->controller_data;
  240. if (!spicfg)
  241. spicfg = &davinci_spi_default_cfg;
  242. if (t) {
  243. bits_per_word = t->bits_per_word;
  244. hz = t->speed_hz;
  245. }
  246. /* if bits_per_word is not set then set it default */
  247. if (!bits_per_word)
  248. bits_per_word = spi->bits_per_word;
  249. /*
  250. * Assign function pointer to appropriate transfer method
  251. * 8bit, 16bit or 32bit transfer
  252. */
  253. if (bits_per_word <= 8) {
  254. dspi->get_rx = davinci_spi_rx_buf_u8;
  255. dspi->get_tx = davinci_spi_tx_buf_u8;
  256. dspi->bytes_per_word[spi->chip_select] = 1;
  257. } else {
  258. dspi->get_rx = davinci_spi_rx_buf_u16;
  259. dspi->get_tx = davinci_spi_tx_buf_u16;
  260. dspi->bytes_per_word[spi->chip_select] = 2;
  261. }
  262. if (!hz)
  263. hz = spi->max_speed_hz;
  264. /* Set up SPIFMTn register, unique to this chipselect. */
  265. prescale = davinci_spi_get_prescale(dspi, hz);
  266. if (prescale < 0)
  267. return prescale;
  268. spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
  269. if (spi->mode & SPI_LSB_FIRST)
  270. spifmt |= SPIFMT_SHIFTDIR_MASK;
  271. if (spi->mode & SPI_CPOL)
  272. spifmt |= SPIFMT_POLARITY_MASK;
  273. if (!(spi->mode & SPI_CPHA))
  274. spifmt |= SPIFMT_PHASE_MASK;
  275. /*
  276. * Assume wdelay is used only on SPI peripherals that has this field
  277. * in SPIFMTn register and when it's configured from board file or DT.
  278. */
  279. if (spicfg->wdelay)
  280. spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
  281. & SPIFMT_WDELAY_MASK);
  282. /*
  283. * Version 1 hardware supports two basic SPI modes:
  284. * - Standard SPI mode uses 4 pins, with chipselect
  285. * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
  286. * (distinct from SPI_3WIRE, with just one data wire;
  287. * or similar variants without MOSI or without MISO)
  288. *
  289. * Version 2 hardware supports an optional handshaking signal,
  290. * so it can support two more modes:
  291. * - 5 pin SPI variant is standard SPI plus SPI_READY
  292. * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
  293. */
  294. if (dspi->version == SPI_VERSION_2) {
  295. u32 delay = 0;
  296. if (spicfg->odd_parity)
  297. spifmt |= SPIFMT_ODD_PARITY_MASK;
  298. if (spicfg->parity_enable)
  299. spifmt |= SPIFMT_PARITYENA_MASK;
  300. if (spicfg->timer_disable) {
  301. spifmt |= SPIFMT_DISTIMER_MASK;
  302. } else {
  303. delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
  304. & SPIDELAY_C2TDELAY_MASK;
  305. delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
  306. & SPIDELAY_T2CDELAY_MASK;
  307. }
  308. if (spi->mode & SPI_READY) {
  309. spifmt |= SPIFMT_WAITENA_MASK;
  310. delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
  311. & SPIDELAY_T2EDELAY_MASK;
  312. delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
  313. & SPIDELAY_C2EDELAY_MASK;
  314. }
  315. iowrite32(delay, dspi->base + SPIDELAY);
  316. }
  317. iowrite32(spifmt, dspi->base + SPIFMT0);
  318. return 0;
  319. }
  320. static int davinci_spi_of_setup(struct spi_device *spi)
  321. {
  322. struct davinci_spi_config *spicfg = spi->controller_data;
  323. struct device_node *np = spi->dev.of_node;
  324. u32 prop;
  325. if (spicfg == NULL && np) {
  326. spicfg = kzalloc(sizeof(*spicfg), GFP_KERNEL);
  327. if (!spicfg)
  328. return -ENOMEM;
  329. *spicfg = davinci_spi_default_cfg;
  330. /* override with dt configured values */
  331. if (!of_property_read_u32(np, "ti,spi-wdelay", &prop))
  332. spicfg->wdelay = (u8)prop;
  333. spi->controller_data = spicfg;
  334. }
  335. return 0;
  336. }
  337. /**
  338. * davinci_spi_setup - This functions will set default transfer method
  339. * @spi: spi device on which data transfer to be done
  340. *
  341. * This functions sets the default transfer method.
  342. */
  343. static int davinci_spi_setup(struct spi_device *spi)
  344. {
  345. int retval = 0;
  346. struct davinci_spi *dspi;
  347. struct davinci_spi_platform_data *pdata;
  348. struct spi_master *master = spi->master;
  349. struct device_node *np = spi->dev.of_node;
  350. bool internal_cs = true;
  351. dspi = spi_master_get_devdata(spi->master);
  352. pdata = &dspi->pdata;
  353. if (!(spi->mode & SPI_NO_CS)) {
  354. if (np && (master->cs_gpios != NULL) && (spi->cs_gpio >= 0)) {
  355. retval = gpio_direction_output(
  356. spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  357. internal_cs = false;
  358. } else if (pdata->chip_sel &&
  359. spi->chip_select < pdata->num_chipselect &&
  360. pdata->chip_sel[spi->chip_select] != SPI_INTERN_CS) {
  361. spi->cs_gpio = pdata->chip_sel[spi->chip_select];
  362. retval = gpio_direction_output(
  363. spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  364. internal_cs = false;
  365. }
  366. if (retval) {
  367. dev_err(&spi->dev, "GPIO %d setup failed (%d)\n",
  368. spi->cs_gpio, retval);
  369. return retval;
  370. }
  371. if (internal_cs)
  372. set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select);
  373. }
  374. if (spi->mode & SPI_READY)
  375. set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK);
  376. if (spi->mode & SPI_LOOP)
  377. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
  378. else
  379. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
  380. return davinci_spi_of_setup(spi);
  381. }
  382. static void davinci_spi_cleanup(struct spi_device *spi)
  383. {
  384. struct davinci_spi_config *spicfg = spi->controller_data;
  385. spi->controller_data = NULL;
  386. if (spi->dev.of_node)
  387. kfree(spicfg);
  388. }
  389. static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
  390. {
  391. struct device *sdev = dspi->bitbang.master->dev.parent;
  392. if (int_status & SPIFLG_TIMEOUT_MASK) {
  393. dev_err(sdev, "SPI Time-out Error\n");
  394. return -ETIMEDOUT;
  395. }
  396. if (int_status & SPIFLG_DESYNC_MASK) {
  397. dev_err(sdev, "SPI Desynchronization Error\n");
  398. return -EIO;
  399. }
  400. if (int_status & SPIFLG_BITERR_MASK) {
  401. dev_err(sdev, "SPI Bit error\n");
  402. return -EIO;
  403. }
  404. if (dspi->version == SPI_VERSION_2) {
  405. if (int_status & SPIFLG_DLEN_ERR_MASK) {
  406. dev_err(sdev, "SPI Data Length Error\n");
  407. return -EIO;
  408. }
  409. if (int_status & SPIFLG_PARERR_MASK) {
  410. dev_err(sdev, "SPI Parity Error\n");
  411. return -EIO;
  412. }
  413. if (int_status & SPIFLG_OVRRUN_MASK) {
  414. dev_err(sdev, "SPI Data Overrun error\n");
  415. return -EIO;
  416. }
  417. if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
  418. dev_err(sdev, "SPI Buffer Init Active\n");
  419. return -EBUSY;
  420. }
  421. }
  422. return 0;
  423. }
  424. /**
  425. * davinci_spi_process_events - check for and handle any SPI controller events
  426. * @dspi: the controller data
  427. *
  428. * This function will check the SPIFLG register and handle any events that are
  429. * detected there
  430. */
  431. static int davinci_spi_process_events(struct davinci_spi *dspi)
  432. {
  433. u32 buf, status, errors = 0, spidat1;
  434. buf = ioread32(dspi->base + SPIBUF);
  435. if (dspi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
  436. dspi->get_rx(buf & 0xFFFF, dspi);
  437. dspi->rcount--;
  438. }
  439. status = ioread32(dspi->base + SPIFLG);
  440. if (unlikely(status & SPIFLG_ERROR_MASK)) {
  441. errors = status & SPIFLG_ERROR_MASK;
  442. goto out;
  443. }
  444. if (dspi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
  445. spidat1 = ioread32(dspi->base + SPIDAT1);
  446. dspi->wcount--;
  447. spidat1 &= ~0xFFFF;
  448. spidat1 |= 0xFFFF & dspi->get_tx(dspi);
  449. iowrite32(spidat1, dspi->base + SPIDAT1);
  450. }
  451. out:
  452. return errors;
  453. }
  454. static void davinci_spi_dma_rx_callback(void *data)
  455. {
  456. struct davinci_spi *dspi = (struct davinci_spi *)data;
  457. dspi->rcount = 0;
  458. if (!dspi->wcount && !dspi->rcount)
  459. complete(&dspi->done);
  460. }
  461. static void davinci_spi_dma_tx_callback(void *data)
  462. {
  463. struct davinci_spi *dspi = (struct davinci_spi *)data;
  464. dspi->wcount = 0;
  465. if (!dspi->wcount && !dspi->rcount)
  466. complete(&dspi->done);
  467. }
  468. /**
  469. * davinci_spi_bufs - functions which will handle transfer data
  470. * @spi: spi device on which data transfer to be done
  471. * @t: spi transfer in which transfer info is filled
  472. *
  473. * This function will put data to be transferred into data register
  474. * of SPI controller and then wait until the completion will be marked
  475. * by the IRQ Handler.
  476. */
  477. static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
  478. {
  479. struct davinci_spi *dspi;
  480. int data_type, ret = -ENOMEM;
  481. u32 tx_data, spidat1;
  482. u32 errors = 0;
  483. struct davinci_spi_config *spicfg;
  484. struct davinci_spi_platform_data *pdata;
  485. unsigned uninitialized_var(rx_buf_count);
  486. void *dummy_buf = NULL;
  487. struct scatterlist sg_rx, sg_tx;
  488. dspi = spi_master_get_devdata(spi->master);
  489. pdata = &dspi->pdata;
  490. spicfg = (struct davinci_spi_config *)spi->controller_data;
  491. if (!spicfg)
  492. spicfg = &davinci_spi_default_cfg;
  493. /* convert len to words based on bits_per_word */
  494. data_type = dspi->bytes_per_word[spi->chip_select];
  495. dspi->tx = t->tx_buf;
  496. dspi->rx = t->rx_buf;
  497. dspi->wcount = t->len / data_type;
  498. dspi->rcount = dspi->wcount;
  499. spidat1 = ioread32(dspi->base + SPIDAT1);
  500. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  501. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  502. reinit_completion(&dspi->done);
  503. if (spicfg->io_type == SPI_IO_TYPE_INTR)
  504. set_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
  505. if (spicfg->io_type != SPI_IO_TYPE_DMA) {
  506. /* start the transfer */
  507. dspi->wcount--;
  508. tx_data = dspi->get_tx(dspi);
  509. spidat1 &= 0xFFFF0000;
  510. spidat1 |= tx_data & 0xFFFF;
  511. iowrite32(spidat1, dspi->base + SPIDAT1);
  512. } else {
  513. struct dma_slave_config dma_rx_conf = {
  514. .direction = DMA_DEV_TO_MEM,
  515. .src_addr = (unsigned long)dspi->pbase + SPIBUF,
  516. .src_addr_width = data_type,
  517. .src_maxburst = 1,
  518. };
  519. struct dma_slave_config dma_tx_conf = {
  520. .direction = DMA_MEM_TO_DEV,
  521. .dst_addr = (unsigned long)dspi->pbase + SPIDAT1,
  522. .dst_addr_width = data_type,
  523. .dst_maxburst = 1,
  524. };
  525. struct dma_async_tx_descriptor *rxdesc;
  526. struct dma_async_tx_descriptor *txdesc;
  527. void *buf;
  528. dummy_buf = kzalloc(t->len, GFP_KERNEL);
  529. if (!dummy_buf)
  530. goto err_alloc_dummy_buf;
  531. dmaengine_slave_config(dspi->dma_rx, &dma_rx_conf);
  532. dmaengine_slave_config(dspi->dma_tx, &dma_tx_conf);
  533. sg_init_table(&sg_rx, 1);
  534. if (!t->rx_buf)
  535. buf = dummy_buf;
  536. else
  537. buf = t->rx_buf;
  538. t->rx_dma = dma_map_single(&spi->dev, buf,
  539. t->len, DMA_FROM_DEVICE);
  540. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  541. ret = -EFAULT;
  542. goto err_rx_map;
  543. }
  544. sg_dma_address(&sg_rx) = t->rx_dma;
  545. sg_dma_len(&sg_rx) = t->len;
  546. sg_init_table(&sg_tx, 1);
  547. if (!t->tx_buf)
  548. buf = dummy_buf;
  549. else
  550. buf = (void *)t->tx_buf;
  551. t->tx_dma = dma_map_single(&spi->dev, buf,
  552. t->len, DMA_TO_DEVICE);
  553. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  554. ret = -EFAULT;
  555. goto err_tx_map;
  556. }
  557. sg_dma_address(&sg_tx) = t->tx_dma;
  558. sg_dma_len(&sg_tx) = t->len;
  559. rxdesc = dmaengine_prep_slave_sg(dspi->dma_rx,
  560. &sg_rx, 1, DMA_DEV_TO_MEM,
  561. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  562. if (!rxdesc)
  563. goto err_desc;
  564. txdesc = dmaengine_prep_slave_sg(dspi->dma_tx,
  565. &sg_tx, 1, DMA_MEM_TO_DEV,
  566. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  567. if (!txdesc)
  568. goto err_desc;
  569. rxdesc->callback = davinci_spi_dma_rx_callback;
  570. rxdesc->callback_param = (void *)dspi;
  571. txdesc->callback = davinci_spi_dma_tx_callback;
  572. txdesc->callback_param = (void *)dspi;
  573. if (pdata->cshold_bug)
  574. iowrite16(spidat1 >> 16, dspi->base + SPIDAT1 + 2);
  575. dmaengine_submit(rxdesc);
  576. dmaengine_submit(txdesc);
  577. dma_async_issue_pending(dspi->dma_rx);
  578. dma_async_issue_pending(dspi->dma_tx);
  579. set_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
  580. }
  581. /* Wait for the transfer to complete */
  582. if (spicfg->io_type != SPI_IO_TYPE_POLL) {
  583. if (wait_for_completion_timeout(&dspi->done, HZ) == 0)
  584. errors = SPIFLG_TIMEOUT_MASK;
  585. } else {
  586. while (dspi->rcount > 0 || dspi->wcount > 0) {
  587. errors = davinci_spi_process_events(dspi);
  588. if (errors)
  589. break;
  590. cpu_relax();
  591. }
  592. }
  593. clear_io_bits(dspi->base + SPIINT, SPIINT_MASKALL);
  594. if (spicfg->io_type == SPI_IO_TYPE_DMA) {
  595. clear_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
  596. dma_unmap_single(&spi->dev, t->rx_dma,
  597. t->len, DMA_FROM_DEVICE);
  598. dma_unmap_single(&spi->dev, t->tx_dma,
  599. t->len, DMA_TO_DEVICE);
  600. kfree(dummy_buf);
  601. }
  602. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  603. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  604. /*
  605. * Check for bit error, desync error,parity error,timeout error and
  606. * receive overflow errors
  607. */
  608. if (errors) {
  609. ret = davinci_spi_check_error(dspi, errors);
  610. WARN(!ret, "%s: error reported but no error found!\n",
  611. dev_name(&spi->dev));
  612. return ret;
  613. }
  614. if (dspi->rcount != 0 || dspi->wcount != 0) {
  615. dev_err(&spi->dev, "SPI data transfer error\n");
  616. return -EIO;
  617. }
  618. return t->len;
  619. err_desc:
  620. dma_unmap_single(&spi->dev, t->tx_dma, t->len, DMA_TO_DEVICE);
  621. err_tx_map:
  622. dma_unmap_single(&spi->dev, t->rx_dma, t->len, DMA_FROM_DEVICE);
  623. err_rx_map:
  624. kfree(dummy_buf);
  625. err_alloc_dummy_buf:
  626. return ret;
  627. }
  628. /**
  629. * dummy_thread_fn - dummy thread function
  630. * @irq: IRQ number for this SPI Master
  631. * @context_data: structure for SPI Master controller davinci_spi
  632. *
  633. * This is to satisfy the request_threaded_irq() API so that the irq
  634. * handler is called in interrupt context.
  635. */
  636. static irqreturn_t dummy_thread_fn(s32 irq, void *data)
  637. {
  638. return IRQ_HANDLED;
  639. }
  640. /**
  641. * davinci_spi_irq - Interrupt handler for SPI Master Controller
  642. * @irq: IRQ number for this SPI Master
  643. * @context_data: structure for SPI Master controller davinci_spi
  644. *
  645. * ISR will determine that interrupt arrives either for READ or WRITE command.
  646. * According to command it will do the appropriate action. It will check
  647. * transfer length and if it is not zero then dispatch transfer command again.
  648. * If transfer length is zero then it will indicate the COMPLETION so that
  649. * davinci_spi_bufs function can go ahead.
  650. */
  651. static irqreturn_t davinci_spi_irq(s32 irq, void *data)
  652. {
  653. struct davinci_spi *dspi = data;
  654. int status;
  655. status = davinci_spi_process_events(dspi);
  656. if (unlikely(status != 0))
  657. clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
  658. if ((!dspi->rcount && !dspi->wcount) || status)
  659. complete(&dspi->done);
  660. return IRQ_HANDLED;
  661. }
  662. static int davinci_spi_request_dma(struct davinci_spi *dspi)
  663. {
  664. struct device *sdev = dspi->bitbang.master->dev.parent;
  665. dspi->dma_rx = dma_request_chan(sdev, "rx");
  666. if (IS_ERR(dspi->dma_rx))
  667. return PTR_ERR(dspi->dma_rx);
  668. dspi->dma_tx = dma_request_chan(sdev, "tx");
  669. if (IS_ERR(dspi->dma_tx)) {
  670. dma_release_channel(dspi->dma_rx);
  671. return PTR_ERR(dspi->dma_tx);
  672. }
  673. return 0;
  674. }
  675. #if defined(CONFIG_OF)
  676. /* OF SPI data structure */
  677. struct davinci_spi_of_data {
  678. u8 version;
  679. u8 prescaler_limit;
  680. };
  681. static const struct davinci_spi_of_data dm6441_spi_data = {
  682. .version = SPI_VERSION_1,
  683. .prescaler_limit = 2,
  684. };
  685. static const struct davinci_spi_of_data da830_spi_data = {
  686. .version = SPI_VERSION_2,
  687. .prescaler_limit = 2,
  688. };
  689. static const struct davinci_spi_of_data keystone_spi_data = {
  690. .version = SPI_VERSION_1,
  691. .prescaler_limit = 0,
  692. };
  693. static const struct of_device_id davinci_spi_of_match[] = {
  694. {
  695. .compatible = "ti,dm6441-spi",
  696. .data = &dm6441_spi_data,
  697. },
  698. {
  699. .compatible = "ti,da830-spi",
  700. .data = &da830_spi_data,
  701. },
  702. {
  703. .compatible = "ti,keystone-spi",
  704. .data = &keystone_spi_data,
  705. },
  706. { },
  707. };
  708. MODULE_DEVICE_TABLE(of, davinci_spi_of_match);
  709. /**
  710. * spi_davinci_get_pdata - Get platform data from DTS binding
  711. * @pdev: ptr to platform data
  712. * @dspi: ptr to driver data
  713. *
  714. * Parses and populates pdata in dspi from device tree bindings.
  715. *
  716. * NOTE: Not all platform data params are supported currently.
  717. */
  718. static int spi_davinci_get_pdata(struct platform_device *pdev,
  719. struct davinci_spi *dspi)
  720. {
  721. struct device_node *node = pdev->dev.of_node;
  722. struct davinci_spi_of_data *spi_data;
  723. struct davinci_spi_platform_data *pdata;
  724. unsigned int num_cs, intr_line = 0;
  725. const struct of_device_id *match;
  726. pdata = &dspi->pdata;
  727. match = of_match_device(davinci_spi_of_match, &pdev->dev);
  728. if (!match)
  729. return -ENODEV;
  730. spi_data = (struct davinci_spi_of_data *)match->data;
  731. pdata->version = spi_data->version;
  732. pdata->prescaler_limit = spi_data->prescaler_limit;
  733. /*
  734. * default num_cs is 1 and all chipsel are internal to the chip
  735. * indicated by chip_sel being NULL or cs_gpios being NULL or
  736. * set to -ENOENT. num-cs includes internal as well as gpios.
  737. * indicated by chip_sel being NULL. GPIO based CS is not
  738. * supported yet in DT bindings.
  739. */
  740. num_cs = 1;
  741. of_property_read_u32(node, "num-cs", &num_cs);
  742. pdata->num_chipselect = num_cs;
  743. of_property_read_u32(node, "ti,davinci-spi-intr-line", &intr_line);
  744. pdata->intr_line = intr_line;
  745. return 0;
  746. }
  747. #else
  748. static struct davinci_spi_platform_data
  749. *spi_davinci_get_pdata(struct platform_device *pdev,
  750. struct davinci_spi *dspi)
  751. {
  752. return -ENODEV;
  753. }
  754. #endif
  755. /**
  756. * davinci_spi_probe - probe function for SPI Master Controller
  757. * @pdev: platform_device structure which contains plateform specific data
  758. *
  759. * According to Linux Device Model this function will be invoked by Linux
  760. * with platform_device struct which contains the device specific info.
  761. * This function will map the SPI controller's memory, register IRQ,
  762. * Reset SPI controller and setting its registers to default value.
  763. * It will invoke spi_bitbang_start to create work queue so that client driver
  764. * can register transfer method to work queue.
  765. */
  766. static int davinci_spi_probe(struct platform_device *pdev)
  767. {
  768. struct spi_master *master;
  769. struct davinci_spi *dspi;
  770. struct davinci_spi_platform_data *pdata;
  771. struct resource *r;
  772. int ret = 0;
  773. u32 spipc0;
  774. master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
  775. if (master == NULL) {
  776. ret = -ENOMEM;
  777. goto err;
  778. }
  779. platform_set_drvdata(pdev, master);
  780. dspi = spi_master_get_devdata(master);
  781. if (dev_get_platdata(&pdev->dev)) {
  782. pdata = dev_get_platdata(&pdev->dev);
  783. dspi->pdata = *pdata;
  784. } else {
  785. /* update dspi pdata with that from the DT */
  786. ret = spi_davinci_get_pdata(pdev, dspi);
  787. if (ret < 0)
  788. goto free_master;
  789. }
  790. /* pdata in dspi is now updated and point pdata to that */
  791. pdata = &dspi->pdata;
  792. dspi->bytes_per_word = devm_kzalloc(&pdev->dev,
  793. sizeof(*dspi->bytes_per_word) *
  794. pdata->num_chipselect, GFP_KERNEL);
  795. if (dspi->bytes_per_word == NULL) {
  796. ret = -ENOMEM;
  797. goto free_master;
  798. }
  799. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  800. if (r == NULL) {
  801. ret = -ENOENT;
  802. goto free_master;
  803. }
  804. dspi->pbase = r->start;
  805. dspi->base = devm_ioremap_resource(&pdev->dev, r);
  806. if (IS_ERR(dspi->base)) {
  807. ret = PTR_ERR(dspi->base);
  808. goto free_master;
  809. }
  810. ret = platform_get_irq(pdev, 0);
  811. if (ret == 0)
  812. ret = -EINVAL;
  813. if (ret < 0)
  814. goto free_master;
  815. dspi->irq = ret;
  816. ret = devm_request_threaded_irq(&pdev->dev, dspi->irq, davinci_spi_irq,
  817. dummy_thread_fn, 0, dev_name(&pdev->dev), dspi);
  818. if (ret)
  819. goto free_master;
  820. dspi->bitbang.master = master;
  821. dspi->clk = devm_clk_get(&pdev->dev, NULL);
  822. if (IS_ERR(dspi->clk)) {
  823. ret = -ENODEV;
  824. goto free_master;
  825. }
  826. clk_prepare_enable(dspi->clk);
  827. master->dev.of_node = pdev->dev.of_node;
  828. master->bus_num = pdev->id;
  829. master->num_chipselect = pdata->num_chipselect;
  830. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 16);
  831. master->setup = davinci_spi_setup;
  832. master->cleanup = davinci_spi_cleanup;
  833. dspi->bitbang.chipselect = davinci_spi_chipselect;
  834. dspi->bitbang.setup_transfer = davinci_spi_setup_transfer;
  835. dspi->prescaler_limit = pdata->prescaler_limit;
  836. dspi->version = pdata->version;
  837. dspi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
  838. if (dspi->version == SPI_VERSION_2)
  839. dspi->bitbang.flags |= SPI_READY;
  840. if (pdev->dev.of_node) {
  841. int i;
  842. for (i = 0; i < pdata->num_chipselect; i++) {
  843. int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
  844. "cs-gpios", i);
  845. if (cs_gpio == -EPROBE_DEFER) {
  846. ret = cs_gpio;
  847. goto free_clk;
  848. }
  849. if (gpio_is_valid(cs_gpio)) {
  850. ret = devm_gpio_request(&pdev->dev, cs_gpio,
  851. dev_name(&pdev->dev));
  852. if (ret)
  853. goto free_clk;
  854. }
  855. }
  856. }
  857. dspi->bitbang.txrx_bufs = davinci_spi_bufs;
  858. ret = davinci_spi_request_dma(dspi);
  859. if (ret == -EPROBE_DEFER) {
  860. goto free_clk;
  861. } else if (ret) {
  862. dev_info(&pdev->dev, "DMA is not supported (%d)\n", ret);
  863. dspi->dma_rx = NULL;
  864. dspi->dma_tx = NULL;
  865. }
  866. dspi->get_rx = davinci_spi_rx_buf_u8;
  867. dspi->get_tx = davinci_spi_tx_buf_u8;
  868. init_completion(&dspi->done);
  869. /* Reset In/OUT SPI module */
  870. iowrite32(0, dspi->base + SPIGCR0);
  871. udelay(100);
  872. iowrite32(1, dspi->base + SPIGCR0);
  873. /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
  874. spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
  875. iowrite32(spipc0, dspi->base + SPIPC0);
  876. if (pdata->intr_line)
  877. iowrite32(SPI_INTLVL_1, dspi->base + SPILVL);
  878. else
  879. iowrite32(SPI_INTLVL_0, dspi->base + SPILVL);
  880. iowrite32(CS_DEFAULT, dspi->base + SPIDEF);
  881. /* master mode default */
  882. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_CLKMOD_MASK);
  883. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
  884. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  885. ret = spi_bitbang_start(&dspi->bitbang);
  886. if (ret)
  887. goto free_dma;
  888. dev_info(&pdev->dev, "Controller at 0x%p\n", dspi->base);
  889. return ret;
  890. free_dma:
  891. if (dspi->dma_rx) {
  892. dma_release_channel(dspi->dma_rx);
  893. dma_release_channel(dspi->dma_tx);
  894. }
  895. free_clk:
  896. clk_disable_unprepare(dspi->clk);
  897. free_master:
  898. spi_master_put(master);
  899. err:
  900. return ret;
  901. }
  902. /**
  903. * davinci_spi_remove - remove function for SPI Master Controller
  904. * @pdev: platform_device structure which contains plateform specific data
  905. *
  906. * This function will do the reverse action of davinci_spi_probe function
  907. * It will free the IRQ and SPI controller's memory region.
  908. * It will also call spi_bitbang_stop to destroy the work queue which was
  909. * created by spi_bitbang_start.
  910. */
  911. static int davinci_spi_remove(struct platform_device *pdev)
  912. {
  913. struct davinci_spi *dspi;
  914. struct spi_master *master;
  915. master = platform_get_drvdata(pdev);
  916. dspi = spi_master_get_devdata(master);
  917. spi_bitbang_stop(&dspi->bitbang);
  918. clk_disable_unprepare(dspi->clk);
  919. spi_master_put(master);
  920. if (dspi->dma_rx) {
  921. dma_release_channel(dspi->dma_rx);
  922. dma_release_channel(dspi->dma_tx);
  923. }
  924. return 0;
  925. }
  926. static struct platform_driver davinci_spi_driver = {
  927. .driver = {
  928. .name = "spi_davinci",
  929. .of_match_table = of_match_ptr(davinci_spi_of_match),
  930. },
  931. .probe = davinci_spi_probe,
  932. .remove = davinci_spi_remove,
  933. };
  934. module_platform_driver(davinci_spi_driver);
  935. MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
  936. MODULE_LICENSE("GPL");