spi-davinci.c 29 KB

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