spi-omap2-mcspi.c 38 KB

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