spi-omap2-mcspi.c 39 KB

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