spi-omap2-mcspi.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  1. /*
  2. * OMAP2 McSPI controller driver
  3. *
  4. * Copyright (C) 2005, 2006 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  6. * Juha Yrj�l� <juha.yrjola@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/device.h>
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/pinctrl/consumer.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/err.h>
  28. #include <linux/clk.h>
  29. #include <linux/io.h>
  30. #include <linux/slab.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/gcd.h>
  35. #include <linux/iopoll.h>
  36. #include <linux/spi/spi.h>
  37. #include <linux/gpio.h>
  38. #include <linux/platform_data/spi-omap2-mcspi.h>
  39. #define OMAP2_MCSPI_MAX_FREQ 48000000
  40. #define OMAP2_MCSPI_MAX_DIVIDER 4096
  41. #define OMAP2_MCSPI_MAX_FIFODEPTH 64
  42. #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
  43. #define SPI_AUTOSUSPEND_TIMEOUT 2000
  44. #define OMAP2_MCSPI_REVISION 0x00
  45. #define OMAP2_MCSPI_SYSSTATUS 0x14
  46. #define OMAP2_MCSPI_IRQSTATUS 0x18
  47. #define OMAP2_MCSPI_IRQENABLE 0x1c
  48. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  49. #define OMAP2_MCSPI_SYST 0x24
  50. #define OMAP2_MCSPI_MODULCTRL 0x28
  51. #define OMAP2_MCSPI_XFERLEVEL 0x7c
  52. /* per-channel banks, 0x14 bytes each, first is: */
  53. #define OMAP2_MCSPI_CHCONF0 0x2c
  54. #define OMAP2_MCSPI_CHSTAT0 0x30
  55. #define OMAP2_MCSPI_CHCTRL0 0x34
  56. #define OMAP2_MCSPI_TX0 0x38
  57. #define OMAP2_MCSPI_RX0 0x3c
  58. /* per-register bitmasks: */
  59. #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
  60. #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
  61. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  62. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  63. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  64. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  65. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  66. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  67. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  68. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  69. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  70. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  71. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  72. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  73. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  74. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  75. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  76. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  77. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  78. #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
  79. #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
  80. #define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
  81. #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
  82. #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
  83. #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
  84. #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
  85. #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
  86. #define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
  87. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  88. /* We have 2 DMA channels per CS, one for RX and one for TX */
  89. struct omap2_mcspi_dma {
  90. struct dma_chan *dma_tx;
  91. struct dma_chan *dma_rx;
  92. struct completion dma_tx_completion;
  93. struct completion dma_rx_completion;
  94. char dma_rx_ch_name[14];
  95. char dma_tx_ch_name[14];
  96. };
  97. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  98. * cache operations; better heuristics consider wordsize and bitrate.
  99. */
  100. #define DMA_MIN_BYTES 160
  101. /*
  102. * Used for context save and restore, structure members to be updated whenever
  103. * corresponding registers are modified.
  104. */
  105. struct omap2_mcspi_regs {
  106. u32 modulctrl;
  107. u32 wakeupenable;
  108. struct list_head cs;
  109. };
  110. struct omap2_mcspi {
  111. struct completion txdone;
  112. struct spi_master *master;
  113. /* Virtual base address of the controller */
  114. void __iomem *base;
  115. unsigned long phys;
  116. /* SPI1 has 4 channels, while SPI2 has 2 */
  117. struct omap2_mcspi_dma *dma_channels;
  118. struct device *dev;
  119. struct omap2_mcspi_regs ctx;
  120. int fifo_depth;
  121. bool slave_aborted;
  122. unsigned int pin_dir:1;
  123. };
  124. struct omap2_mcspi_cs {
  125. void __iomem *base;
  126. unsigned long phys;
  127. int word_len;
  128. u16 mode;
  129. struct list_head node;
  130. /* Context save and restore shadow register */
  131. u32 chconf0, chctrl0;
  132. };
  133. static inline void mcspi_write_reg(struct spi_master *master,
  134. int idx, u32 val)
  135. {
  136. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  137. writel_relaxed(val, mcspi->base + idx);
  138. }
  139. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  140. {
  141. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  142. return readl_relaxed(mcspi->base + idx);
  143. }
  144. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  145. int idx, u32 val)
  146. {
  147. struct omap2_mcspi_cs *cs = spi->controller_state;
  148. writel_relaxed(val, cs->base + idx);
  149. }
  150. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  151. {
  152. struct omap2_mcspi_cs *cs = spi->controller_state;
  153. return readl_relaxed(cs->base + idx);
  154. }
  155. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  156. {
  157. struct omap2_mcspi_cs *cs = spi->controller_state;
  158. return cs->chconf0;
  159. }
  160. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  161. {
  162. struct omap2_mcspi_cs *cs = spi->controller_state;
  163. cs->chconf0 = val;
  164. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  165. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  166. }
  167. static inline int mcspi_bytes_per_word(int word_len)
  168. {
  169. if (word_len <= 8)
  170. return 1;
  171. else if (word_len <= 16)
  172. return 2;
  173. else /* word_len <= 32 */
  174. return 4;
  175. }
  176. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  177. int is_read, int enable)
  178. {
  179. u32 l, rw;
  180. l = mcspi_cached_chconf0(spi);
  181. if (is_read) /* 1 is read, 0 write */
  182. rw = OMAP2_MCSPI_CHCONF_DMAR;
  183. else
  184. rw = OMAP2_MCSPI_CHCONF_DMAW;
  185. if (enable)
  186. l |= rw;
  187. else
  188. l &= ~rw;
  189. mcspi_write_chconf0(spi, l);
  190. }
  191. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  192. {
  193. struct omap2_mcspi_cs *cs = spi->controller_state;
  194. u32 l;
  195. l = cs->chctrl0;
  196. if (enable)
  197. l |= OMAP2_MCSPI_CHCTRL_EN;
  198. else
  199. l &= ~OMAP2_MCSPI_CHCTRL_EN;
  200. cs->chctrl0 = l;
  201. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  202. /* Flash post-writes */
  203. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  204. }
  205. static void omap2_mcspi_set_cs(struct spi_device *spi, bool enable)
  206. {
  207. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  208. u32 l;
  209. /* The controller handles the inverted chip selects
  210. * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
  211. * the inversion from the core spi_set_cs function.
  212. */
  213. if (spi->mode & SPI_CS_HIGH)
  214. enable = !enable;
  215. if (spi->controller_state) {
  216. int err = pm_runtime_get_sync(mcspi->dev);
  217. if (err < 0) {
  218. pm_runtime_put_noidle(mcspi->dev);
  219. dev_err(mcspi->dev, "failed to get sync: %d\n", err);
  220. return;
  221. }
  222. l = mcspi_cached_chconf0(spi);
  223. if (enable)
  224. l &= ~OMAP2_MCSPI_CHCONF_FORCE;
  225. else
  226. l |= OMAP2_MCSPI_CHCONF_FORCE;
  227. mcspi_write_chconf0(spi, l);
  228. pm_runtime_mark_last_busy(mcspi->dev);
  229. pm_runtime_put_autosuspend(mcspi->dev);
  230. }
  231. }
  232. static void omap2_mcspi_set_mode(struct spi_master *master)
  233. {
  234. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  235. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  236. u32 l;
  237. /*
  238. * Choose master or slave mode
  239. */
  240. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  241. l &= ~(OMAP2_MCSPI_MODULCTRL_STEST);
  242. if (spi_controller_is_slave(master)) {
  243. l |= (OMAP2_MCSPI_MODULCTRL_MS);
  244. } else {
  245. l &= ~(OMAP2_MCSPI_MODULCTRL_MS);
  246. l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  247. }
  248. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  249. ctx->modulctrl = l;
  250. }
  251. static void omap2_mcspi_set_fifo(const struct spi_device *spi,
  252. struct spi_transfer *t, int enable)
  253. {
  254. struct spi_master *master = spi->master;
  255. struct omap2_mcspi_cs *cs = spi->controller_state;
  256. struct omap2_mcspi *mcspi;
  257. unsigned int wcnt;
  258. int max_fifo_depth, bytes_per_word;
  259. u32 chconf, xferlevel;
  260. mcspi = spi_master_get_devdata(master);
  261. chconf = mcspi_cached_chconf0(spi);
  262. if (enable) {
  263. bytes_per_word = mcspi_bytes_per_word(cs->word_len);
  264. if (t->len % bytes_per_word != 0)
  265. goto disable_fifo;
  266. if (t->rx_buf != NULL && t->tx_buf != NULL)
  267. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
  268. else
  269. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
  270. wcnt = t->len / bytes_per_word;
  271. if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
  272. goto disable_fifo;
  273. xferlevel = wcnt << 16;
  274. if (t->rx_buf != NULL) {
  275. chconf |= OMAP2_MCSPI_CHCONF_FFER;
  276. xferlevel |= (bytes_per_word - 1) << 8;
  277. }
  278. if (t->tx_buf != NULL) {
  279. chconf |= OMAP2_MCSPI_CHCONF_FFET;
  280. xferlevel |= bytes_per_word - 1;
  281. }
  282. mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
  283. mcspi_write_chconf0(spi, chconf);
  284. mcspi->fifo_depth = max_fifo_depth;
  285. return;
  286. }
  287. disable_fifo:
  288. if (t->rx_buf != NULL)
  289. chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
  290. if (t->tx_buf != NULL)
  291. chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
  292. mcspi_write_chconf0(spi, chconf);
  293. mcspi->fifo_depth = 0;
  294. }
  295. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  296. {
  297. u32 val;
  298. return readl_poll_timeout(reg, val, val & bit, 1, MSEC_PER_SEC);
  299. }
  300. static int mcspi_wait_for_completion(struct omap2_mcspi *mcspi,
  301. struct completion *x)
  302. {
  303. if (spi_controller_is_slave(mcspi->master)) {
  304. if (wait_for_completion_interruptible(x) ||
  305. mcspi->slave_aborted)
  306. return -EINTR;
  307. } else {
  308. wait_for_completion(x);
  309. }
  310. return 0;
  311. }
  312. static void omap2_mcspi_rx_callback(void *data)
  313. {
  314. struct spi_device *spi = data;
  315. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  316. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  317. /* We must disable the DMA RX request */
  318. omap2_mcspi_set_dma_req(spi, 1, 0);
  319. complete(&mcspi_dma->dma_rx_completion);
  320. }
  321. static void omap2_mcspi_tx_callback(void *data)
  322. {
  323. struct spi_device *spi = data;
  324. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  325. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  326. /* We must disable the DMA TX request */
  327. omap2_mcspi_set_dma_req(spi, 0, 0);
  328. complete(&mcspi_dma->dma_tx_completion);
  329. }
  330. static void omap2_mcspi_tx_dma(struct spi_device *spi,
  331. struct spi_transfer *xfer,
  332. struct dma_slave_config cfg)
  333. {
  334. struct omap2_mcspi *mcspi;
  335. struct omap2_mcspi_dma *mcspi_dma;
  336. mcspi = spi_master_get_devdata(spi->master);
  337. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  338. if (mcspi_dma->dma_tx) {
  339. struct dma_async_tx_descriptor *tx;
  340. dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
  341. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, xfer->tx_sg.sgl,
  342. xfer->tx_sg.nents,
  343. DMA_MEM_TO_DEV,
  344. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  345. if (tx) {
  346. tx->callback = omap2_mcspi_tx_callback;
  347. tx->callback_param = spi;
  348. dmaengine_submit(tx);
  349. } else {
  350. /* FIXME: fall back to PIO? */
  351. }
  352. }
  353. dma_async_issue_pending(mcspi_dma->dma_tx);
  354. omap2_mcspi_set_dma_req(spi, 0, 1);
  355. }
  356. static unsigned
  357. omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
  358. struct dma_slave_config cfg,
  359. unsigned es)
  360. {
  361. struct omap2_mcspi *mcspi;
  362. struct omap2_mcspi_dma *mcspi_dma;
  363. unsigned int count, transfer_reduction = 0;
  364. struct scatterlist *sg_out[2];
  365. int nb_sizes = 0, out_mapped_nents[2], ret, x;
  366. size_t sizes[2];
  367. u32 l;
  368. int elements = 0;
  369. int word_len, element_count;
  370. struct omap2_mcspi_cs *cs = spi->controller_state;
  371. void __iomem *chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  372. mcspi = spi_master_get_devdata(spi->master);
  373. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  374. count = xfer->len;
  375. /*
  376. * In the "End-of-Transfer Procedure" section for DMA RX in OMAP35x TRM
  377. * it mentions reducing DMA transfer length by one element in master
  378. * normal mode.
  379. */
  380. if (mcspi->fifo_depth == 0)
  381. transfer_reduction = es;
  382. word_len = cs->word_len;
  383. l = mcspi_cached_chconf0(spi);
  384. if (word_len <= 8)
  385. element_count = count;
  386. else if (word_len <= 16)
  387. element_count = count >> 1;
  388. else /* word_len <= 32 */
  389. element_count = count >> 2;
  390. if (mcspi_dma->dma_rx) {
  391. struct dma_async_tx_descriptor *tx;
  392. dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
  393. /*
  394. * Reduce DMA transfer length by one more if McSPI is
  395. * configured in turbo mode.
  396. */
  397. if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
  398. transfer_reduction += es;
  399. if (transfer_reduction) {
  400. /* Split sgl into two. The second sgl won't be used. */
  401. sizes[0] = count - transfer_reduction;
  402. sizes[1] = transfer_reduction;
  403. nb_sizes = 2;
  404. } else {
  405. /*
  406. * Don't bother splitting the sgl. This essentially
  407. * clones the original sgl.
  408. */
  409. sizes[0] = count;
  410. nb_sizes = 1;
  411. }
  412. ret = sg_split(xfer->rx_sg.sgl, xfer->rx_sg.nents,
  413. 0, nb_sizes,
  414. sizes,
  415. sg_out, out_mapped_nents,
  416. GFP_KERNEL);
  417. if (ret < 0) {
  418. dev_err(&spi->dev, "sg_split failed\n");
  419. return 0;
  420. }
  421. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx,
  422. sg_out[0],
  423. out_mapped_nents[0],
  424. DMA_DEV_TO_MEM,
  425. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  426. if (tx) {
  427. tx->callback = omap2_mcspi_rx_callback;
  428. tx->callback_param = spi;
  429. dmaengine_submit(tx);
  430. } else {
  431. /* FIXME: fall back to PIO? */
  432. }
  433. }
  434. dma_async_issue_pending(mcspi_dma->dma_rx);
  435. omap2_mcspi_set_dma_req(spi, 1, 1);
  436. ret = mcspi_wait_for_completion(mcspi, &mcspi_dma->dma_rx_completion);
  437. if (ret || mcspi->slave_aborted) {
  438. dmaengine_terminate_sync(mcspi_dma->dma_rx);
  439. omap2_mcspi_set_dma_req(spi, 1, 0);
  440. return 0;
  441. }
  442. for (x = 0; x < nb_sizes; x++)
  443. kfree(sg_out[x]);
  444. if (mcspi->fifo_depth > 0)
  445. return count;
  446. /*
  447. * Due to the DMA transfer length reduction the missing bytes must
  448. * be read manually to receive all of the expected data.
  449. */
  450. omap2_mcspi_set_enable(spi, 0);
  451. elements = element_count - 1;
  452. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  453. elements--;
  454. if (!mcspi_wait_for_reg_bit(chstat_reg,
  455. OMAP2_MCSPI_CHSTAT_RXS)) {
  456. u32 w;
  457. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  458. if (word_len <= 8)
  459. ((u8 *)xfer->rx_buf)[elements++] = w;
  460. else if (word_len <= 16)
  461. ((u16 *)xfer->rx_buf)[elements++] = w;
  462. else /* word_len <= 32 */
  463. ((u32 *)xfer->rx_buf)[elements++] = w;
  464. } else {
  465. int bytes_per_word = mcspi_bytes_per_word(word_len);
  466. dev_err(&spi->dev, "DMA RX penultimate word empty\n");
  467. count -= (bytes_per_word << 1);
  468. omap2_mcspi_set_enable(spi, 1);
  469. return count;
  470. }
  471. }
  472. if (!mcspi_wait_for_reg_bit(chstat_reg, OMAP2_MCSPI_CHSTAT_RXS)) {
  473. u32 w;
  474. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  475. if (word_len <= 8)
  476. ((u8 *)xfer->rx_buf)[elements] = w;
  477. else if (word_len <= 16)
  478. ((u16 *)xfer->rx_buf)[elements] = w;
  479. else /* word_len <= 32 */
  480. ((u32 *)xfer->rx_buf)[elements] = w;
  481. } else {
  482. dev_err(&spi->dev, "DMA RX last word empty\n");
  483. count -= mcspi_bytes_per_word(word_len);
  484. }
  485. omap2_mcspi_set_enable(spi, 1);
  486. return count;
  487. }
  488. static unsigned
  489. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  490. {
  491. struct omap2_mcspi *mcspi;
  492. struct omap2_mcspi_cs *cs = spi->controller_state;
  493. struct omap2_mcspi_dma *mcspi_dma;
  494. unsigned int count;
  495. u8 *rx;
  496. const u8 *tx;
  497. struct dma_slave_config cfg;
  498. enum dma_slave_buswidth width;
  499. unsigned es;
  500. void __iomem *chstat_reg;
  501. void __iomem *irqstat_reg;
  502. int wait_res;
  503. mcspi = spi_master_get_devdata(spi->master);
  504. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  505. if (cs->word_len <= 8) {
  506. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  507. es = 1;
  508. } else if (cs->word_len <= 16) {
  509. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  510. es = 2;
  511. } else {
  512. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  513. es = 4;
  514. }
  515. count = xfer->len;
  516. memset(&cfg, 0, sizeof(cfg));
  517. cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
  518. cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
  519. cfg.src_addr_width = width;
  520. cfg.dst_addr_width = width;
  521. cfg.src_maxburst = es;
  522. cfg.dst_maxburst = es;
  523. rx = xfer->rx_buf;
  524. tx = xfer->tx_buf;
  525. mcspi->slave_aborted = false;
  526. reinit_completion(&mcspi_dma->dma_tx_completion);
  527. reinit_completion(&mcspi_dma->dma_rx_completion);
  528. reinit_completion(&mcspi->txdone);
  529. if (tx) {
  530. /* Enable EOW IRQ to know end of tx in slave mode */
  531. if (spi_controller_is_slave(spi->master))
  532. mcspi_write_reg(spi->master,
  533. OMAP2_MCSPI_IRQENABLE,
  534. OMAP2_MCSPI_IRQSTATUS_EOW);
  535. omap2_mcspi_tx_dma(spi, xfer, cfg);
  536. }
  537. if (rx != NULL)
  538. count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
  539. if (tx != NULL) {
  540. int ret;
  541. ret = mcspi_wait_for_completion(mcspi, &mcspi_dma->dma_tx_completion);
  542. if (ret || mcspi->slave_aborted) {
  543. dmaengine_terminate_sync(mcspi_dma->dma_tx);
  544. omap2_mcspi_set_dma_req(spi, 0, 0);
  545. return 0;
  546. }
  547. if (spi_controller_is_slave(mcspi->master)) {
  548. ret = mcspi_wait_for_completion(mcspi, &mcspi->txdone);
  549. if (ret || mcspi->slave_aborted)
  550. return 0;
  551. }
  552. if (mcspi->fifo_depth > 0) {
  553. irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
  554. if (mcspi_wait_for_reg_bit(irqstat_reg,
  555. OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
  556. dev_err(&spi->dev, "EOW timed out\n");
  557. mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
  558. OMAP2_MCSPI_IRQSTATUS_EOW);
  559. }
  560. /* for TX_ONLY mode, be sure all words have shifted out */
  561. if (rx == NULL) {
  562. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  563. if (mcspi->fifo_depth > 0) {
  564. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  565. OMAP2_MCSPI_CHSTAT_TXFFE);
  566. if (wait_res < 0)
  567. dev_err(&spi->dev, "TXFFE timed out\n");
  568. } else {
  569. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  570. OMAP2_MCSPI_CHSTAT_TXS);
  571. if (wait_res < 0)
  572. dev_err(&spi->dev, "TXS timed out\n");
  573. }
  574. if (wait_res >= 0 &&
  575. (mcspi_wait_for_reg_bit(chstat_reg,
  576. OMAP2_MCSPI_CHSTAT_EOT) < 0))
  577. dev_err(&spi->dev, "EOT timed out\n");
  578. }
  579. }
  580. return count;
  581. }
  582. static unsigned
  583. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  584. {
  585. struct omap2_mcspi_cs *cs = spi->controller_state;
  586. unsigned int count, c;
  587. u32 l;
  588. void __iomem *base = cs->base;
  589. void __iomem *tx_reg;
  590. void __iomem *rx_reg;
  591. void __iomem *chstat_reg;
  592. int word_len;
  593. count = xfer->len;
  594. c = count;
  595. word_len = cs->word_len;
  596. l = mcspi_cached_chconf0(spi);
  597. /* We store the pre-calculated register addresses on stack to speed
  598. * up the transfer loop. */
  599. tx_reg = base + OMAP2_MCSPI_TX0;
  600. rx_reg = base + OMAP2_MCSPI_RX0;
  601. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  602. if (c < (word_len>>3))
  603. return 0;
  604. if (word_len <= 8) {
  605. u8 *rx;
  606. const u8 *tx;
  607. rx = xfer->rx_buf;
  608. tx = xfer->tx_buf;
  609. do {
  610. c -= 1;
  611. if (tx != NULL) {
  612. if (mcspi_wait_for_reg_bit(chstat_reg,
  613. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  614. dev_err(&spi->dev, "TXS timed out\n");
  615. goto out;
  616. }
  617. dev_vdbg(&spi->dev, "write-%d %02x\n",
  618. word_len, *tx);
  619. writel_relaxed(*tx++, tx_reg);
  620. }
  621. if (rx != NULL) {
  622. if (mcspi_wait_for_reg_bit(chstat_reg,
  623. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  624. dev_err(&spi->dev, "RXS timed out\n");
  625. goto out;
  626. }
  627. if (c == 1 && tx == NULL &&
  628. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  629. omap2_mcspi_set_enable(spi, 0);
  630. *rx++ = readl_relaxed(rx_reg);
  631. dev_vdbg(&spi->dev, "read-%d %02x\n",
  632. word_len, *(rx - 1));
  633. if (mcspi_wait_for_reg_bit(chstat_reg,
  634. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  635. dev_err(&spi->dev,
  636. "RXS timed out\n");
  637. goto out;
  638. }
  639. c = 0;
  640. } else if (c == 0 && tx == NULL) {
  641. omap2_mcspi_set_enable(spi, 0);
  642. }
  643. *rx++ = readl_relaxed(rx_reg);
  644. dev_vdbg(&spi->dev, "read-%d %02x\n",
  645. word_len, *(rx - 1));
  646. }
  647. } while (c);
  648. } else if (word_len <= 16) {
  649. u16 *rx;
  650. const u16 *tx;
  651. rx = xfer->rx_buf;
  652. tx = xfer->tx_buf;
  653. do {
  654. c -= 2;
  655. if (tx != NULL) {
  656. if (mcspi_wait_for_reg_bit(chstat_reg,
  657. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  658. dev_err(&spi->dev, "TXS timed out\n");
  659. goto out;
  660. }
  661. dev_vdbg(&spi->dev, "write-%d %04x\n",
  662. word_len, *tx);
  663. writel_relaxed(*tx++, tx_reg);
  664. }
  665. if (rx != NULL) {
  666. if (mcspi_wait_for_reg_bit(chstat_reg,
  667. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  668. dev_err(&spi->dev, "RXS timed out\n");
  669. goto out;
  670. }
  671. if (c == 2 && tx == NULL &&
  672. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  673. omap2_mcspi_set_enable(spi, 0);
  674. *rx++ = readl_relaxed(rx_reg);
  675. dev_vdbg(&spi->dev, "read-%d %04x\n",
  676. word_len, *(rx - 1));
  677. if (mcspi_wait_for_reg_bit(chstat_reg,
  678. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  679. dev_err(&spi->dev,
  680. "RXS timed out\n");
  681. goto out;
  682. }
  683. c = 0;
  684. } else if (c == 0 && tx == NULL) {
  685. omap2_mcspi_set_enable(spi, 0);
  686. }
  687. *rx++ = readl_relaxed(rx_reg);
  688. dev_vdbg(&spi->dev, "read-%d %04x\n",
  689. word_len, *(rx - 1));
  690. }
  691. } while (c >= 2);
  692. } else if (word_len <= 32) {
  693. u32 *rx;
  694. const u32 *tx;
  695. rx = xfer->rx_buf;
  696. tx = xfer->tx_buf;
  697. do {
  698. c -= 4;
  699. if (tx != NULL) {
  700. if (mcspi_wait_for_reg_bit(chstat_reg,
  701. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  702. dev_err(&spi->dev, "TXS timed out\n");
  703. goto out;
  704. }
  705. dev_vdbg(&spi->dev, "write-%d %08x\n",
  706. word_len, *tx);
  707. writel_relaxed(*tx++, tx_reg);
  708. }
  709. if (rx != NULL) {
  710. if (mcspi_wait_for_reg_bit(chstat_reg,
  711. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  712. dev_err(&spi->dev, "RXS timed out\n");
  713. goto out;
  714. }
  715. if (c == 4 && tx == NULL &&
  716. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  717. omap2_mcspi_set_enable(spi, 0);
  718. *rx++ = readl_relaxed(rx_reg);
  719. dev_vdbg(&spi->dev, "read-%d %08x\n",
  720. word_len, *(rx - 1));
  721. if (mcspi_wait_for_reg_bit(chstat_reg,
  722. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  723. dev_err(&spi->dev,
  724. "RXS timed out\n");
  725. goto out;
  726. }
  727. c = 0;
  728. } else if (c == 0 && tx == NULL) {
  729. omap2_mcspi_set_enable(spi, 0);
  730. }
  731. *rx++ = readl_relaxed(rx_reg);
  732. dev_vdbg(&spi->dev, "read-%d %08x\n",
  733. word_len, *(rx - 1));
  734. }
  735. } while (c >= 4);
  736. }
  737. /* for TX_ONLY mode, be sure all words have shifted out */
  738. if (xfer->rx_buf == NULL) {
  739. if (mcspi_wait_for_reg_bit(chstat_reg,
  740. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  741. dev_err(&spi->dev, "TXS timed out\n");
  742. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  743. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  744. dev_err(&spi->dev, "EOT timed out\n");
  745. /* disable chan to purge rx datas received in TX_ONLY transfer,
  746. * otherwise these rx datas will affect the direct following
  747. * RX_ONLY transfer.
  748. */
  749. omap2_mcspi_set_enable(spi, 0);
  750. }
  751. out:
  752. omap2_mcspi_set_enable(spi, 1);
  753. return count - c;
  754. }
  755. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  756. {
  757. u32 div;
  758. for (div = 0; div < 15; div++)
  759. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  760. return div;
  761. return 15;
  762. }
  763. /* called only when no transfer is active to this device */
  764. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  765. struct spi_transfer *t)
  766. {
  767. struct omap2_mcspi_cs *cs = spi->controller_state;
  768. struct omap2_mcspi *mcspi;
  769. u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
  770. u8 word_len = spi->bits_per_word;
  771. u32 speed_hz = spi->max_speed_hz;
  772. mcspi = spi_master_get_devdata(spi->master);
  773. if (t != NULL && t->bits_per_word)
  774. word_len = t->bits_per_word;
  775. cs->word_len = word_len;
  776. if (t && t->speed_hz)
  777. speed_hz = t->speed_hz;
  778. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  779. if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
  780. clkd = omap2_mcspi_calc_divisor(speed_hz);
  781. speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
  782. clkg = 0;
  783. } else {
  784. div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
  785. speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
  786. clkd = (div - 1) & 0xf;
  787. extclk = (div - 1) >> 4;
  788. clkg = OMAP2_MCSPI_CHCONF_CLKG;
  789. }
  790. l = mcspi_cached_chconf0(spi);
  791. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  792. * REVISIT: this controller could support SPI_3WIRE mode.
  793. */
  794. if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
  795. l &= ~OMAP2_MCSPI_CHCONF_IS;
  796. l &= ~OMAP2_MCSPI_CHCONF_DPE1;
  797. l |= OMAP2_MCSPI_CHCONF_DPE0;
  798. } else {
  799. l |= OMAP2_MCSPI_CHCONF_IS;
  800. l |= OMAP2_MCSPI_CHCONF_DPE1;
  801. l &= ~OMAP2_MCSPI_CHCONF_DPE0;
  802. }
  803. /* wordlength */
  804. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  805. l |= (word_len - 1) << 7;
  806. /* set chipselect polarity; manage with FORCE */
  807. if (!(spi->mode & SPI_CS_HIGH))
  808. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  809. else
  810. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  811. /* set clock divisor */
  812. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  813. l |= clkd << 2;
  814. /* set clock granularity */
  815. l &= ~OMAP2_MCSPI_CHCONF_CLKG;
  816. l |= clkg;
  817. if (clkg) {
  818. cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
  819. cs->chctrl0 |= extclk << 8;
  820. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  821. }
  822. /* set SPI mode 0..3 */
  823. if (spi->mode & SPI_CPOL)
  824. l |= OMAP2_MCSPI_CHCONF_POL;
  825. else
  826. l &= ~OMAP2_MCSPI_CHCONF_POL;
  827. if (spi->mode & SPI_CPHA)
  828. l |= OMAP2_MCSPI_CHCONF_PHA;
  829. else
  830. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  831. mcspi_write_chconf0(spi, l);
  832. cs->mode = spi->mode;
  833. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  834. speed_hz,
  835. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  836. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  837. return 0;
  838. }
  839. /*
  840. * Note that we currently allow DMA only if we get a channel
  841. * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
  842. */
  843. static int omap2_mcspi_request_dma(struct spi_device *spi)
  844. {
  845. struct spi_master *master = spi->master;
  846. struct omap2_mcspi *mcspi;
  847. struct omap2_mcspi_dma *mcspi_dma;
  848. int ret = 0;
  849. mcspi = spi_master_get_devdata(master);
  850. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  851. init_completion(&mcspi_dma->dma_rx_completion);
  852. init_completion(&mcspi_dma->dma_tx_completion);
  853. mcspi_dma->dma_rx = dma_request_chan(&master->dev,
  854. mcspi_dma->dma_rx_ch_name);
  855. if (IS_ERR(mcspi_dma->dma_rx)) {
  856. ret = PTR_ERR(mcspi_dma->dma_rx);
  857. mcspi_dma->dma_rx = NULL;
  858. goto no_dma;
  859. }
  860. mcspi_dma->dma_tx = dma_request_chan(&master->dev,
  861. mcspi_dma->dma_tx_ch_name);
  862. if (IS_ERR(mcspi_dma->dma_tx)) {
  863. ret = PTR_ERR(mcspi_dma->dma_tx);
  864. mcspi_dma->dma_tx = NULL;
  865. dma_release_channel(mcspi_dma->dma_rx);
  866. mcspi_dma->dma_rx = NULL;
  867. }
  868. no_dma:
  869. return ret;
  870. }
  871. static int omap2_mcspi_setup(struct spi_device *spi)
  872. {
  873. int ret;
  874. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  875. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  876. struct omap2_mcspi_dma *mcspi_dma;
  877. struct omap2_mcspi_cs *cs = spi->controller_state;
  878. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  879. if (!cs) {
  880. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  881. if (!cs)
  882. return -ENOMEM;
  883. cs->base = mcspi->base + spi->chip_select * 0x14;
  884. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  885. cs->mode = 0;
  886. cs->chconf0 = 0;
  887. cs->chctrl0 = 0;
  888. spi->controller_state = cs;
  889. /* Link this to context save list */
  890. list_add_tail(&cs->node, &ctx->cs);
  891. if (gpio_is_valid(spi->cs_gpio)) {
  892. ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
  893. if (ret) {
  894. dev_err(&spi->dev, "failed to request gpio\n");
  895. return ret;
  896. }
  897. gpio_direction_output(spi->cs_gpio,
  898. !(spi->mode & SPI_CS_HIGH));
  899. }
  900. }
  901. if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
  902. ret = omap2_mcspi_request_dma(spi);
  903. if (ret)
  904. dev_warn(&spi->dev, "not using DMA for McSPI (%d)\n",
  905. ret);
  906. }
  907. ret = pm_runtime_get_sync(mcspi->dev);
  908. if (ret < 0) {
  909. pm_runtime_put_noidle(mcspi->dev);
  910. return ret;
  911. }
  912. ret = omap2_mcspi_setup_transfer(spi, NULL);
  913. pm_runtime_mark_last_busy(mcspi->dev);
  914. pm_runtime_put_autosuspend(mcspi->dev);
  915. return ret;
  916. }
  917. static void omap2_mcspi_cleanup(struct spi_device *spi)
  918. {
  919. struct omap2_mcspi *mcspi;
  920. struct omap2_mcspi_dma *mcspi_dma;
  921. struct omap2_mcspi_cs *cs;
  922. mcspi = spi_master_get_devdata(spi->master);
  923. if (spi->controller_state) {
  924. /* Unlink controller state from context save list */
  925. cs = spi->controller_state;
  926. list_del(&cs->node);
  927. kfree(cs);
  928. }
  929. if (spi->chip_select < spi->master->num_chipselect) {
  930. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  931. if (mcspi_dma->dma_rx) {
  932. dma_release_channel(mcspi_dma->dma_rx);
  933. mcspi_dma->dma_rx = NULL;
  934. }
  935. if (mcspi_dma->dma_tx) {
  936. dma_release_channel(mcspi_dma->dma_tx);
  937. mcspi_dma->dma_tx = NULL;
  938. }
  939. }
  940. if (gpio_is_valid(spi->cs_gpio))
  941. gpio_free(spi->cs_gpio);
  942. }
  943. static irqreturn_t omap2_mcspi_irq_handler(int irq, void *data)
  944. {
  945. struct omap2_mcspi *mcspi = data;
  946. u32 irqstat;
  947. irqstat = mcspi_read_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS);
  948. if (!irqstat)
  949. return IRQ_NONE;
  950. /* Disable IRQ and wakeup slave xfer task */
  951. mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQENABLE, 0);
  952. if (irqstat & OMAP2_MCSPI_IRQSTATUS_EOW)
  953. complete(&mcspi->txdone);
  954. return IRQ_HANDLED;
  955. }
  956. static int omap2_mcspi_slave_abort(struct spi_master *master)
  957. {
  958. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  959. struct omap2_mcspi_dma *mcspi_dma = mcspi->dma_channels;
  960. mcspi->slave_aborted = true;
  961. complete(&mcspi_dma->dma_rx_completion);
  962. complete(&mcspi_dma->dma_tx_completion);
  963. complete(&mcspi->txdone);
  964. return 0;
  965. }
  966. static int omap2_mcspi_transfer_one(struct spi_master *master,
  967. struct spi_device *spi,
  968. struct spi_transfer *t)
  969. {
  970. /* We only enable one channel at a time -- the one whose message is
  971. * -- although this controller would gladly
  972. * arbitrate among multiple channels. This corresponds to "single
  973. * channel" master mode. As a side effect, we need to manage the
  974. * chipselect with the FORCE bit ... CS != channel enable.
  975. */
  976. struct omap2_mcspi *mcspi;
  977. struct omap2_mcspi_dma *mcspi_dma;
  978. struct omap2_mcspi_cs *cs;
  979. struct omap2_mcspi_device_config *cd;
  980. int par_override = 0;
  981. int status = 0;
  982. u32 chconf;
  983. mcspi = spi_master_get_devdata(master);
  984. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  985. cs = spi->controller_state;
  986. cd = spi->controller_data;
  987. /*
  988. * The slave driver could have changed spi->mode in which case
  989. * it will be different from cs->mode (the current hardware setup).
  990. * If so, set par_override (even though its not a parity issue) so
  991. * omap2_mcspi_setup_transfer will be called to configure the hardware
  992. * with the correct mode on the first iteration of the loop below.
  993. */
  994. if (spi->mode != cs->mode)
  995. par_override = 1;
  996. omap2_mcspi_set_enable(spi, 0);
  997. if (gpio_is_valid(spi->cs_gpio))
  998. omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH);
  999. if (par_override ||
  1000. (t->speed_hz != spi->max_speed_hz) ||
  1001. (t->bits_per_word != spi->bits_per_word)) {
  1002. par_override = 1;
  1003. status = omap2_mcspi_setup_transfer(spi, t);
  1004. if (status < 0)
  1005. goto out;
  1006. if (t->speed_hz == spi->max_speed_hz &&
  1007. t->bits_per_word == spi->bits_per_word)
  1008. par_override = 0;
  1009. }
  1010. if (cd && cd->cs_per_word) {
  1011. chconf = mcspi->ctx.modulctrl;
  1012. chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
  1013. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  1014. mcspi->ctx.modulctrl =
  1015. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  1016. }
  1017. chconf = mcspi_cached_chconf0(spi);
  1018. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  1019. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  1020. if (t->tx_buf == NULL)
  1021. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  1022. else if (t->rx_buf == NULL)
  1023. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  1024. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  1025. /* Turbo mode is for more than one word */
  1026. if (t->len > ((cs->word_len + 7) >> 3))
  1027. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  1028. }
  1029. mcspi_write_chconf0(spi, chconf);
  1030. if (t->len) {
  1031. unsigned count;
  1032. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  1033. master->cur_msg_mapped &&
  1034. master->can_dma(master, spi, t))
  1035. omap2_mcspi_set_fifo(spi, t, 1);
  1036. omap2_mcspi_set_enable(spi, 1);
  1037. /* RX_ONLY mode needs dummy data in TX reg */
  1038. if (t->tx_buf == NULL)
  1039. writel_relaxed(0, cs->base
  1040. + OMAP2_MCSPI_TX0);
  1041. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  1042. master->cur_msg_mapped &&
  1043. master->can_dma(master, spi, t))
  1044. count = omap2_mcspi_txrx_dma(spi, t);
  1045. else
  1046. count = omap2_mcspi_txrx_pio(spi, t);
  1047. if (count != t->len) {
  1048. status = -EIO;
  1049. goto out;
  1050. }
  1051. }
  1052. omap2_mcspi_set_enable(spi, 0);
  1053. if (mcspi->fifo_depth > 0)
  1054. omap2_mcspi_set_fifo(spi, t, 0);
  1055. out:
  1056. /* Restore defaults if they were overriden */
  1057. if (par_override) {
  1058. par_override = 0;
  1059. status = omap2_mcspi_setup_transfer(spi, NULL);
  1060. }
  1061. if (cd && cd->cs_per_word) {
  1062. chconf = mcspi->ctx.modulctrl;
  1063. chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  1064. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  1065. mcspi->ctx.modulctrl =
  1066. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  1067. }
  1068. omap2_mcspi_set_enable(spi, 0);
  1069. if (gpio_is_valid(spi->cs_gpio))
  1070. omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH));
  1071. if (mcspi->fifo_depth > 0 && t)
  1072. omap2_mcspi_set_fifo(spi, t, 0);
  1073. return status;
  1074. }
  1075. static int omap2_mcspi_prepare_message(struct spi_master *master,
  1076. struct spi_message *msg)
  1077. {
  1078. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1079. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1080. struct omap2_mcspi_cs *cs;
  1081. /* Only a single channel can have the FORCE bit enabled
  1082. * in its chconf0 register.
  1083. * Scan all channels and disable them except the current one.
  1084. * A FORCE can remain from a last transfer having cs_change enabled
  1085. */
  1086. list_for_each_entry(cs, &ctx->cs, node) {
  1087. if (msg->spi->controller_state == cs)
  1088. continue;
  1089. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE)) {
  1090. cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
  1091. writel_relaxed(cs->chconf0,
  1092. cs->base + OMAP2_MCSPI_CHCONF0);
  1093. readl_relaxed(cs->base + OMAP2_MCSPI_CHCONF0);
  1094. }
  1095. }
  1096. return 0;
  1097. }
  1098. static bool omap2_mcspi_can_dma(struct spi_master *master,
  1099. struct spi_device *spi,
  1100. struct spi_transfer *xfer)
  1101. {
  1102. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  1103. struct omap2_mcspi_dma *mcspi_dma =
  1104. &mcspi->dma_channels[spi->chip_select];
  1105. if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx)
  1106. return false;
  1107. if (spi_controller_is_slave(master))
  1108. return true;
  1109. return (xfer->len >= DMA_MIN_BYTES);
  1110. }
  1111. static int omap2_mcspi_controller_setup(struct omap2_mcspi *mcspi)
  1112. {
  1113. struct spi_master *master = mcspi->master;
  1114. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1115. int ret = 0;
  1116. ret = pm_runtime_get_sync(mcspi->dev);
  1117. if (ret < 0) {
  1118. pm_runtime_put_noidle(mcspi->dev);
  1119. return ret;
  1120. }
  1121. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
  1122. OMAP2_MCSPI_WAKEUPENABLE_WKEN);
  1123. ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  1124. omap2_mcspi_set_mode(master);
  1125. pm_runtime_mark_last_busy(mcspi->dev);
  1126. pm_runtime_put_autosuspend(mcspi->dev);
  1127. return 0;
  1128. }
  1129. /*
  1130. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1131. * inactive state when driver was suspend, then force it to inactive state at
  1132. * wake up.
  1133. */
  1134. static int omap_mcspi_runtime_resume(struct device *dev)
  1135. {
  1136. struct spi_master *master = dev_get_drvdata(dev);
  1137. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1138. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1139. struct omap2_mcspi_cs *cs;
  1140. /* McSPI: context restore */
  1141. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
  1142. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
  1143. list_for_each_entry(cs, &ctx->cs, node) {
  1144. /*
  1145. * We need to toggle CS state for OMAP take this
  1146. * change in account.
  1147. */
  1148. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1149. cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
  1150. writel_relaxed(cs->chconf0,
  1151. cs->base + OMAP2_MCSPI_CHCONF0);
  1152. cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
  1153. writel_relaxed(cs->chconf0,
  1154. cs->base + OMAP2_MCSPI_CHCONF0);
  1155. } else {
  1156. writel_relaxed(cs->chconf0,
  1157. cs->base + OMAP2_MCSPI_CHCONF0);
  1158. }
  1159. }
  1160. return 0;
  1161. }
  1162. static struct omap2_mcspi_platform_config omap2_pdata = {
  1163. .regs_offset = 0,
  1164. };
  1165. static struct omap2_mcspi_platform_config omap4_pdata = {
  1166. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  1167. };
  1168. static const struct of_device_id omap_mcspi_of_match[] = {
  1169. {
  1170. .compatible = "ti,omap2-mcspi",
  1171. .data = &omap2_pdata,
  1172. },
  1173. {
  1174. .compatible = "ti,omap4-mcspi",
  1175. .data = &omap4_pdata,
  1176. },
  1177. { },
  1178. };
  1179. MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
  1180. static int omap2_mcspi_probe(struct platform_device *pdev)
  1181. {
  1182. struct spi_master *master;
  1183. const struct omap2_mcspi_platform_config *pdata;
  1184. struct omap2_mcspi *mcspi;
  1185. struct resource *r;
  1186. int status = 0, i;
  1187. u32 regs_offset = 0;
  1188. struct device_node *node = pdev->dev.of_node;
  1189. const struct of_device_id *match;
  1190. if (of_property_read_bool(node, "spi-slave"))
  1191. master = spi_alloc_slave(&pdev->dev, sizeof(*mcspi));
  1192. else
  1193. master = spi_alloc_master(&pdev->dev, sizeof(*mcspi));
  1194. if (!master)
  1195. return -ENOMEM;
  1196. /* the spi->mode bits understood by this driver: */
  1197. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1198. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1199. master->setup = omap2_mcspi_setup;
  1200. master->auto_runtime_pm = true;
  1201. master->prepare_message = omap2_mcspi_prepare_message;
  1202. master->can_dma = omap2_mcspi_can_dma;
  1203. master->transfer_one = omap2_mcspi_transfer_one;
  1204. master->set_cs = omap2_mcspi_set_cs;
  1205. master->cleanup = omap2_mcspi_cleanup;
  1206. master->slave_abort = omap2_mcspi_slave_abort;
  1207. master->dev.of_node = node;
  1208. master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
  1209. master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
  1210. platform_set_drvdata(pdev, master);
  1211. mcspi = spi_master_get_devdata(master);
  1212. mcspi->master = master;
  1213. match = of_match_device(omap_mcspi_of_match, &pdev->dev);
  1214. if (match) {
  1215. u32 num_cs = 1; /* default number of chipselect */
  1216. pdata = match->data;
  1217. of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
  1218. master->num_chipselect = num_cs;
  1219. if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
  1220. mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
  1221. } else {
  1222. pdata = dev_get_platdata(&pdev->dev);
  1223. master->num_chipselect = pdata->num_cs;
  1224. mcspi->pin_dir = pdata->pin_dir;
  1225. }
  1226. regs_offset = pdata->regs_offset;
  1227. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1228. mcspi->base = devm_ioremap_resource(&pdev->dev, r);
  1229. if (IS_ERR(mcspi->base)) {
  1230. status = PTR_ERR(mcspi->base);
  1231. goto free_master;
  1232. }
  1233. mcspi->phys = r->start + regs_offset;
  1234. mcspi->base += regs_offset;
  1235. mcspi->dev = &pdev->dev;
  1236. INIT_LIST_HEAD(&mcspi->ctx.cs);
  1237. mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
  1238. sizeof(struct omap2_mcspi_dma),
  1239. GFP_KERNEL);
  1240. if (mcspi->dma_channels == NULL) {
  1241. status = -ENOMEM;
  1242. goto free_master;
  1243. }
  1244. for (i = 0; i < master->num_chipselect; i++) {
  1245. sprintf(mcspi->dma_channels[i].dma_rx_ch_name, "rx%d", i);
  1246. sprintf(mcspi->dma_channels[i].dma_tx_ch_name, "tx%d", i);
  1247. }
  1248. status = platform_get_irq(pdev, 0);
  1249. if (status == -EPROBE_DEFER)
  1250. goto free_master;
  1251. if (status < 0) {
  1252. dev_err(&pdev->dev, "no irq resource found\n");
  1253. goto free_master;
  1254. }
  1255. init_completion(&mcspi->txdone);
  1256. status = devm_request_irq(&pdev->dev, status,
  1257. omap2_mcspi_irq_handler, 0, pdev->name,
  1258. mcspi);
  1259. if (status) {
  1260. dev_err(&pdev->dev, "Cannot request IRQ");
  1261. goto free_master;
  1262. }
  1263. pm_runtime_use_autosuspend(&pdev->dev);
  1264. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  1265. pm_runtime_enable(&pdev->dev);
  1266. status = omap2_mcspi_controller_setup(mcspi);
  1267. if (status < 0)
  1268. goto disable_pm;
  1269. status = devm_spi_register_controller(&pdev->dev, master);
  1270. if (status < 0)
  1271. goto disable_pm;
  1272. return status;
  1273. disable_pm:
  1274. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1275. pm_runtime_put_sync(&pdev->dev);
  1276. pm_runtime_disable(&pdev->dev);
  1277. free_master:
  1278. spi_master_put(master);
  1279. return status;
  1280. }
  1281. static int omap2_mcspi_remove(struct platform_device *pdev)
  1282. {
  1283. struct spi_master *master = platform_get_drvdata(pdev);
  1284. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1285. pm_runtime_dont_use_autosuspend(mcspi->dev);
  1286. pm_runtime_put_sync(mcspi->dev);
  1287. pm_runtime_disable(&pdev->dev);
  1288. return 0;
  1289. }
  1290. /* work with hotplug and coldplug */
  1291. MODULE_ALIAS("platform:omap2_mcspi");
  1292. static int __maybe_unused omap2_mcspi_suspend(struct device *dev)
  1293. {
  1294. struct spi_master *master = dev_get_drvdata(dev);
  1295. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1296. int error;
  1297. error = pinctrl_pm_select_sleep_state(dev);
  1298. if (error)
  1299. dev_warn(mcspi->dev, "%s: failed to set pins: %i\n",
  1300. __func__, error);
  1301. error = spi_master_suspend(master);
  1302. if (error)
  1303. dev_warn(mcspi->dev, "%s: master suspend failed: %i\n",
  1304. __func__, error);
  1305. return pm_runtime_force_suspend(dev);
  1306. }
  1307. static int __maybe_unused omap2_mcspi_resume(struct device *dev)
  1308. {
  1309. struct spi_master *master = dev_get_drvdata(dev);
  1310. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1311. int error;
  1312. error = pinctrl_pm_select_default_state(dev);
  1313. if (error)
  1314. dev_warn(mcspi->dev, "%s: failed to set pins: %i\n",
  1315. __func__, error);
  1316. error = spi_master_resume(master);
  1317. if (error)
  1318. dev_warn(mcspi->dev, "%s: master resume failed: %i\n",
  1319. __func__, error);
  1320. return pm_runtime_force_resume(dev);
  1321. }
  1322. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1323. SET_SYSTEM_SLEEP_PM_OPS(omap2_mcspi_suspend,
  1324. omap2_mcspi_resume)
  1325. .runtime_resume = omap_mcspi_runtime_resume,
  1326. };
  1327. static struct platform_driver omap2_mcspi_driver = {
  1328. .driver = {
  1329. .name = "omap2_mcspi",
  1330. .pm = &omap2_mcspi_pm_ops,
  1331. .of_match_table = omap_mcspi_of_match,
  1332. },
  1333. .probe = omap2_mcspi_probe,
  1334. .remove = omap2_mcspi_remove,
  1335. };
  1336. module_platform_driver(omap2_mcspi_driver);
  1337. MODULE_LICENSE("GPL");