spi-omap2-mcspi.c 33 KB

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