omap2_mcspi.c 34 KB

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