spi-fsl-spi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. /*
  2. * Freescale SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. * Copyright 2010 Freescale Semiconductor, Inc.
  8. *
  9. * CPM SPI and QE buffer descriptors mode support:
  10. * Copyright (c) 2009 MontaVista Software, Inc.
  11. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  12. *
  13. * GRLIB support:
  14. * Copyright (c) 2012 Aeroflex Gaisler AB.
  15. * Author: Andreas Larsson <andreas@gaisler.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify it
  18. * under the terms of the GNU General Public License as published by the
  19. * Free Software Foundation; either version 2 of the License, or (at your
  20. * option) any later version.
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/fsl_devices.h>
  25. #include <linux/gpio.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/irq.h>
  28. #include <linux/kernel.h>
  29. #include <linux/mm.h>
  30. #include <linux/module.h>
  31. #include <linux/mutex.h>
  32. #include <linux/of.h>
  33. #include <linux/of_address.h>
  34. #include <linux/of_irq.h>
  35. #include <linux/of_gpio.h>
  36. #include <linux/of_platform.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/spi/spi.h>
  39. #include <linux/spi/spi_bitbang.h>
  40. #include <linux/types.h>
  41. #include "spi-fsl-lib.h"
  42. #include "spi-fsl-cpm.h"
  43. #include "spi-fsl-spi.h"
  44. #define TYPE_FSL 0
  45. #define TYPE_GRLIB 1
  46. struct fsl_spi_match_data {
  47. int type;
  48. };
  49. static struct fsl_spi_match_data of_fsl_spi_fsl_config = {
  50. .type = TYPE_FSL,
  51. };
  52. static struct fsl_spi_match_data of_fsl_spi_grlib_config = {
  53. .type = TYPE_GRLIB,
  54. };
  55. static const struct of_device_id of_fsl_spi_match[] = {
  56. {
  57. .compatible = "fsl,spi",
  58. .data = &of_fsl_spi_fsl_config,
  59. },
  60. {
  61. .compatible = "aeroflexgaisler,spictrl",
  62. .data = &of_fsl_spi_grlib_config,
  63. },
  64. {}
  65. };
  66. MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
  67. static int fsl_spi_get_type(struct device *dev)
  68. {
  69. const struct of_device_id *match;
  70. if (dev->of_node) {
  71. match = of_match_node(of_fsl_spi_match, dev->of_node);
  72. if (match && match->data)
  73. return ((struct fsl_spi_match_data *)match->data)->type;
  74. }
  75. return TYPE_FSL;
  76. }
  77. static void fsl_spi_change_mode(struct spi_device *spi)
  78. {
  79. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  80. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  81. struct fsl_spi_reg *reg_base = mspi->reg_base;
  82. __be32 __iomem *mode = &reg_base->mode;
  83. unsigned long flags;
  84. if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
  85. return;
  86. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  87. local_irq_save(flags);
  88. /* Turn off SPI unit prior changing mode */
  89. mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
  90. /* When in CPM mode, we need to reinit tx and rx. */
  91. if (mspi->flags & SPI_CPM_MODE) {
  92. fsl_spi_cpm_reinit_txrx(mspi);
  93. }
  94. mpc8xxx_spi_write_reg(mode, cs->hw_mode);
  95. local_irq_restore(flags);
  96. }
  97. static void fsl_spi_chipselect(struct spi_device *spi, int value)
  98. {
  99. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  100. struct fsl_spi_platform_data *pdata;
  101. bool pol = spi->mode & SPI_CS_HIGH;
  102. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  103. pdata = spi->dev.parent->parent->platform_data;
  104. if (value == BITBANG_CS_INACTIVE) {
  105. if (pdata->cs_control)
  106. pdata->cs_control(spi, !pol);
  107. }
  108. if (value == BITBANG_CS_ACTIVE) {
  109. mpc8xxx_spi->rx_shift = cs->rx_shift;
  110. mpc8xxx_spi->tx_shift = cs->tx_shift;
  111. mpc8xxx_spi->get_rx = cs->get_rx;
  112. mpc8xxx_spi->get_tx = cs->get_tx;
  113. fsl_spi_change_mode(spi);
  114. if (pdata->cs_control)
  115. pdata->cs_control(spi, pol);
  116. }
  117. }
  118. static void fsl_spi_qe_cpu_set_shifts(u32 *rx_shift, u32 *tx_shift,
  119. int bits_per_word, int msb_first)
  120. {
  121. *rx_shift = 0;
  122. *tx_shift = 0;
  123. if (msb_first) {
  124. if (bits_per_word <= 8) {
  125. *rx_shift = 16;
  126. *tx_shift = 24;
  127. } else if (bits_per_word <= 16) {
  128. *rx_shift = 16;
  129. *tx_shift = 16;
  130. }
  131. } else {
  132. if (bits_per_word <= 8)
  133. *rx_shift = 8;
  134. }
  135. }
  136. static void fsl_spi_grlib_set_shifts(u32 *rx_shift, u32 *tx_shift,
  137. int bits_per_word, int msb_first)
  138. {
  139. *rx_shift = 0;
  140. *tx_shift = 0;
  141. if (bits_per_word <= 16) {
  142. if (msb_first) {
  143. *rx_shift = 16; /* LSB in bit 16 */
  144. *tx_shift = 32 - bits_per_word; /* MSB in bit 31 */
  145. } else {
  146. *rx_shift = 16 - bits_per_word; /* MSB in bit 15 */
  147. }
  148. }
  149. }
  150. static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
  151. struct spi_device *spi,
  152. struct mpc8xxx_spi *mpc8xxx_spi,
  153. int bits_per_word)
  154. {
  155. cs->rx_shift = 0;
  156. cs->tx_shift = 0;
  157. if (bits_per_word <= 8) {
  158. cs->get_rx = mpc8xxx_spi_rx_buf_u8;
  159. cs->get_tx = mpc8xxx_spi_tx_buf_u8;
  160. } else if (bits_per_word <= 16) {
  161. cs->get_rx = mpc8xxx_spi_rx_buf_u16;
  162. cs->get_tx = mpc8xxx_spi_tx_buf_u16;
  163. } else if (bits_per_word <= 32) {
  164. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  165. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  166. } else
  167. return -EINVAL;
  168. if (mpc8xxx_spi->set_shifts)
  169. mpc8xxx_spi->set_shifts(&cs->rx_shift, &cs->tx_shift,
  170. bits_per_word,
  171. !(spi->mode & SPI_LSB_FIRST));
  172. mpc8xxx_spi->rx_shift = cs->rx_shift;
  173. mpc8xxx_spi->tx_shift = cs->tx_shift;
  174. mpc8xxx_spi->get_rx = cs->get_rx;
  175. mpc8xxx_spi->get_tx = cs->get_tx;
  176. return bits_per_word;
  177. }
  178. static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
  179. struct spi_device *spi,
  180. int bits_per_word)
  181. {
  182. /* QE uses Little Endian for words > 8
  183. * so transform all words > 8 into 8 bits
  184. * Unfortnatly that doesn't work for LSB so
  185. * reject these for now */
  186. /* Note: 32 bits word, LSB works iff
  187. * tfcr/rfcr is set to CPMFCR_GBL */
  188. if (spi->mode & SPI_LSB_FIRST &&
  189. bits_per_word > 8)
  190. return -EINVAL;
  191. if (bits_per_word > 8)
  192. return 8; /* pretend its 8 bits */
  193. return bits_per_word;
  194. }
  195. static int fsl_spi_setup_transfer(struct spi_device *spi,
  196. struct spi_transfer *t)
  197. {
  198. struct mpc8xxx_spi *mpc8xxx_spi;
  199. int bits_per_word = 0;
  200. u8 pm;
  201. u32 hz = 0;
  202. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  203. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  204. if (t) {
  205. bits_per_word = t->bits_per_word;
  206. hz = t->speed_hz;
  207. }
  208. /* spi_transfer level calls that work per-word */
  209. if (!bits_per_word)
  210. bits_per_word = spi->bits_per_word;
  211. if (!hz)
  212. hz = spi->max_speed_hz;
  213. if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
  214. bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
  215. mpc8xxx_spi,
  216. bits_per_word);
  217. else if (mpc8xxx_spi->flags & SPI_QE)
  218. bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
  219. bits_per_word);
  220. if (bits_per_word < 0)
  221. return bits_per_word;
  222. if (bits_per_word == 32)
  223. bits_per_word = 0;
  224. else
  225. bits_per_word = bits_per_word - 1;
  226. /* mask out bits we are going to set */
  227. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  228. | SPMODE_PM(0xF));
  229. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  230. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  231. cs->hw_mode |= SPMODE_DIV16;
  232. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
  233. WARN_ONCE(pm > 16,
  234. "%s: Requested speed is too low: %d Hz. Will use %d Hz instead.\n",
  235. dev_name(&spi->dev), hz, mpc8xxx_spi->spibrg / 1024);
  236. if (pm > 16)
  237. pm = 16;
  238. } else {
  239. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
  240. }
  241. if (pm)
  242. pm--;
  243. cs->hw_mode |= SPMODE_PM(pm);
  244. fsl_spi_change_mode(spi);
  245. return 0;
  246. }
  247. static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
  248. struct spi_transfer *t, unsigned int len)
  249. {
  250. u32 word;
  251. struct fsl_spi_reg *reg_base = mspi->reg_base;
  252. mspi->count = len;
  253. /* enable rx ints */
  254. mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
  255. /* transmit word */
  256. word = mspi->get_tx(mspi);
  257. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  258. return 0;
  259. }
  260. static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
  261. bool is_dma_mapped)
  262. {
  263. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  264. struct fsl_spi_reg *reg_base;
  265. unsigned int len = t->len;
  266. u8 bits_per_word;
  267. int ret;
  268. reg_base = mpc8xxx_spi->reg_base;
  269. bits_per_word = spi->bits_per_word;
  270. if (t->bits_per_word)
  271. bits_per_word = t->bits_per_word;
  272. if (bits_per_word > 8) {
  273. /* invalid length? */
  274. if (len & 1)
  275. return -EINVAL;
  276. len /= 2;
  277. }
  278. if (bits_per_word > 16) {
  279. /* invalid length? */
  280. if (len & 1)
  281. return -EINVAL;
  282. len /= 2;
  283. }
  284. mpc8xxx_spi->tx = t->tx_buf;
  285. mpc8xxx_spi->rx = t->rx_buf;
  286. reinit_completion(&mpc8xxx_spi->done);
  287. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  288. ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
  289. else
  290. ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
  291. if (ret)
  292. return ret;
  293. wait_for_completion(&mpc8xxx_spi->done);
  294. /* disable rx ints */
  295. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  296. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  297. fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
  298. return mpc8xxx_spi->count;
  299. }
  300. static int fsl_spi_do_one_msg(struct spi_master *master,
  301. struct spi_message *m)
  302. {
  303. struct spi_device *spi = m->spi;
  304. struct spi_transfer *t, *first;
  305. unsigned int cs_change;
  306. const int nsecs = 50;
  307. int status;
  308. /* Don't allow changes if CS is active */
  309. first = list_first_entry(&m->transfers, struct spi_transfer,
  310. transfer_list);
  311. list_for_each_entry(t, &m->transfers, transfer_list) {
  312. if ((first->bits_per_word != t->bits_per_word) ||
  313. (first->speed_hz != t->speed_hz)) {
  314. dev_err(&spi->dev,
  315. "bits_per_word/speed_hz should be same for the same SPI transfer\n");
  316. return -EINVAL;
  317. }
  318. }
  319. cs_change = 1;
  320. status = -EINVAL;
  321. list_for_each_entry(t, &m->transfers, transfer_list) {
  322. if (t->bits_per_word || t->speed_hz) {
  323. if (cs_change)
  324. status = fsl_spi_setup_transfer(spi, t);
  325. if (status < 0)
  326. break;
  327. }
  328. if (cs_change) {
  329. fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  330. ndelay(nsecs);
  331. }
  332. cs_change = t->cs_change;
  333. if (t->len)
  334. status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
  335. if (status) {
  336. status = -EMSGSIZE;
  337. break;
  338. }
  339. m->actual_length += t->len;
  340. if (t->delay_usecs)
  341. udelay(t->delay_usecs);
  342. if (cs_change) {
  343. ndelay(nsecs);
  344. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  345. ndelay(nsecs);
  346. }
  347. }
  348. m->status = status;
  349. if (status || !cs_change) {
  350. ndelay(nsecs);
  351. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  352. }
  353. fsl_spi_setup_transfer(spi, NULL);
  354. spi_finalize_current_message(master);
  355. return 0;
  356. }
  357. static int fsl_spi_setup(struct spi_device *spi)
  358. {
  359. struct mpc8xxx_spi *mpc8xxx_spi;
  360. struct fsl_spi_reg *reg_base;
  361. int retval;
  362. u32 hw_mode;
  363. struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
  364. if (!spi->max_speed_hz)
  365. return -EINVAL;
  366. if (!cs) {
  367. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  368. if (!cs)
  369. return -ENOMEM;
  370. spi_set_ctldata(spi, cs);
  371. }
  372. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  373. reg_base = mpc8xxx_spi->reg_base;
  374. hw_mode = cs->hw_mode; /* Save original settings */
  375. cs->hw_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
  376. /* mask out bits we are going to set */
  377. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  378. | SPMODE_REV | SPMODE_LOOP);
  379. if (spi->mode & SPI_CPHA)
  380. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  381. if (spi->mode & SPI_CPOL)
  382. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  383. if (!(spi->mode & SPI_LSB_FIRST))
  384. cs->hw_mode |= SPMODE_REV;
  385. if (spi->mode & SPI_LOOP)
  386. cs->hw_mode |= SPMODE_LOOP;
  387. retval = fsl_spi_setup_transfer(spi, NULL);
  388. if (retval < 0) {
  389. cs->hw_mode = hw_mode; /* Restore settings */
  390. return retval;
  391. }
  392. if (mpc8xxx_spi->type == TYPE_GRLIB) {
  393. if (gpio_is_valid(spi->cs_gpio)) {
  394. int desel;
  395. retval = gpio_request(spi->cs_gpio,
  396. dev_name(&spi->dev));
  397. if (retval)
  398. return retval;
  399. desel = !(spi->mode & SPI_CS_HIGH);
  400. retval = gpio_direction_output(spi->cs_gpio, desel);
  401. if (retval) {
  402. gpio_free(spi->cs_gpio);
  403. return retval;
  404. }
  405. } else if (spi->cs_gpio != -ENOENT) {
  406. if (spi->cs_gpio < 0)
  407. return spi->cs_gpio;
  408. return -EINVAL;
  409. }
  410. /* When spi->cs_gpio == -ENOENT, a hole in the phandle list
  411. * indicates to use native chipselect if present, or allow for
  412. * an always selected chip
  413. */
  414. }
  415. /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
  416. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  417. return 0;
  418. }
  419. static void fsl_spi_cleanup(struct spi_device *spi)
  420. {
  421. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  422. struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
  423. if (mpc8xxx_spi->type == TYPE_GRLIB && gpio_is_valid(spi->cs_gpio))
  424. gpio_free(spi->cs_gpio);
  425. kfree(cs);
  426. spi_set_ctldata(spi, NULL);
  427. }
  428. static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  429. {
  430. struct fsl_spi_reg *reg_base = mspi->reg_base;
  431. /* We need handle RX first */
  432. if (events & SPIE_NE) {
  433. u32 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
  434. if (mspi->rx)
  435. mspi->get_rx(rx_data, mspi);
  436. }
  437. if ((events & SPIE_NF) == 0)
  438. /* spin until TX is done */
  439. while (((events =
  440. mpc8xxx_spi_read_reg(&reg_base->event)) &
  441. SPIE_NF) == 0)
  442. cpu_relax();
  443. /* Clear the events */
  444. mpc8xxx_spi_write_reg(&reg_base->event, events);
  445. mspi->count -= 1;
  446. if (mspi->count) {
  447. u32 word = mspi->get_tx(mspi);
  448. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  449. } else {
  450. complete(&mspi->done);
  451. }
  452. }
  453. static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
  454. {
  455. struct mpc8xxx_spi *mspi = context_data;
  456. irqreturn_t ret = IRQ_NONE;
  457. u32 events;
  458. struct fsl_spi_reg *reg_base = mspi->reg_base;
  459. /* Get interrupt events(tx/rx) */
  460. events = mpc8xxx_spi_read_reg(&reg_base->event);
  461. if (events)
  462. ret = IRQ_HANDLED;
  463. dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
  464. if (mspi->flags & SPI_CPM_MODE)
  465. fsl_spi_cpm_irq(mspi, events);
  466. else
  467. fsl_spi_cpu_irq(mspi, events);
  468. return ret;
  469. }
  470. static void fsl_spi_grlib_cs_control(struct spi_device *spi, bool on)
  471. {
  472. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  473. struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
  474. u32 slvsel;
  475. u16 cs = spi->chip_select;
  476. if (gpio_is_valid(spi->cs_gpio)) {
  477. gpio_set_value(spi->cs_gpio, on);
  478. } else if (cs < mpc8xxx_spi->native_chipselects) {
  479. slvsel = mpc8xxx_spi_read_reg(&reg_base->slvsel);
  480. slvsel = on ? (slvsel | (1 << cs)) : (slvsel & ~(1 << cs));
  481. mpc8xxx_spi_write_reg(&reg_base->slvsel, slvsel);
  482. }
  483. }
  484. static void fsl_spi_grlib_probe(struct device *dev)
  485. {
  486. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  487. struct spi_master *master = dev_get_drvdata(dev);
  488. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
  489. struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
  490. int mbits;
  491. u32 capabilities;
  492. capabilities = mpc8xxx_spi_read_reg(&reg_base->cap);
  493. mpc8xxx_spi->set_shifts = fsl_spi_grlib_set_shifts;
  494. mbits = SPCAP_MAXWLEN(capabilities);
  495. if (mbits)
  496. mpc8xxx_spi->max_bits_per_word = mbits + 1;
  497. mpc8xxx_spi->native_chipselects = 0;
  498. if (SPCAP_SSEN(capabilities)) {
  499. mpc8xxx_spi->native_chipselects = SPCAP_SSSZ(capabilities);
  500. mpc8xxx_spi_write_reg(&reg_base->slvsel, 0xffffffff);
  501. }
  502. master->num_chipselect = mpc8xxx_spi->native_chipselects;
  503. pdata->cs_control = fsl_spi_grlib_cs_control;
  504. }
  505. static struct spi_master * fsl_spi_probe(struct device *dev,
  506. struct resource *mem, unsigned int irq)
  507. {
  508. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  509. struct spi_master *master;
  510. struct mpc8xxx_spi *mpc8xxx_spi;
  511. struct fsl_spi_reg *reg_base;
  512. u32 regval;
  513. int ret = 0;
  514. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  515. if (master == NULL) {
  516. ret = -ENOMEM;
  517. goto err;
  518. }
  519. dev_set_drvdata(dev, master);
  520. mpc8xxx_spi_probe(dev, mem, irq);
  521. master->setup = fsl_spi_setup;
  522. master->cleanup = fsl_spi_cleanup;
  523. master->transfer_one_message = fsl_spi_do_one_msg;
  524. mpc8xxx_spi = spi_master_get_devdata(master);
  525. mpc8xxx_spi->max_bits_per_word = 32;
  526. mpc8xxx_spi->type = fsl_spi_get_type(dev);
  527. ret = fsl_spi_cpm_init(mpc8xxx_spi);
  528. if (ret)
  529. goto err_cpm_init;
  530. mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
  531. if (IS_ERR(mpc8xxx_spi->reg_base)) {
  532. ret = PTR_ERR(mpc8xxx_spi->reg_base);
  533. goto err_probe;
  534. }
  535. if (mpc8xxx_spi->type == TYPE_GRLIB)
  536. fsl_spi_grlib_probe(dev);
  537. master->bits_per_word_mask =
  538. (SPI_BPW_RANGE_MASK(4, 16) | SPI_BPW_MASK(32)) &
  539. SPI_BPW_RANGE_MASK(1, mpc8xxx_spi->max_bits_per_word);
  540. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  541. mpc8xxx_spi->set_shifts = fsl_spi_qe_cpu_set_shifts;
  542. if (mpc8xxx_spi->set_shifts)
  543. /* 8 bits per word and MSB first */
  544. mpc8xxx_spi->set_shifts(&mpc8xxx_spi->rx_shift,
  545. &mpc8xxx_spi->tx_shift, 8, 1);
  546. /* Register for SPI Interrupt */
  547. ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_spi_irq,
  548. 0, "fsl_spi", mpc8xxx_spi);
  549. if (ret != 0)
  550. goto err_probe;
  551. reg_base = mpc8xxx_spi->reg_base;
  552. /* SPI controller initializations */
  553. mpc8xxx_spi_write_reg(&reg_base->mode, 0);
  554. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  555. mpc8xxx_spi_write_reg(&reg_base->command, 0);
  556. mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
  557. /* Enable SPI interface */
  558. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  559. if (mpc8xxx_spi->max_bits_per_word < 8) {
  560. regval &= ~SPMODE_LEN(0xF);
  561. regval |= SPMODE_LEN(mpc8xxx_spi->max_bits_per_word - 1);
  562. }
  563. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  564. regval |= SPMODE_OP;
  565. mpc8xxx_spi_write_reg(&reg_base->mode, regval);
  566. ret = devm_spi_register_master(dev, master);
  567. if (ret < 0)
  568. goto err_probe;
  569. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
  570. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  571. return master;
  572. err_probe:
  573. fsl_spi_cpm_free(mpc8xxx_spi);
  574. err_cpm_init:
  575. spi_master_put(master);
  576. err:
  577. return ERR_PTR(ret);
  578. }
  579. static void fsl_spi_cs_control(struct spi_device *spi, bool on)
  580. {
  581. struct device *dev = spi->dev.parent->parent;
  582. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  583. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  584. u16 cs = spi->chip_select;
  585. int gpio = pinfo->gpios[cs];
  586. bool alow = pinfo->alow_flags[cs];
  587. gpio_set_value(gpio, on ^ alow);
  588. }
  589. static int of_fsl_spi_get_chipselects(struct device *dev)
  590. {
  591. struct device_node *np = dev->of_node;
  592. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  593. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  594. int ngpios;
  595. int i = 0;
  596. int ret;
  597. ngpios = of_gpio_count(np);
  598. if (ngpios <= 0) {
  599. /*
  600. * SPI w/o chip-select line. One SPI device is still permitted
  601. * though.
  602. */
  603. pdata->max_chipselect = 1;
  604. return 0;
  605. }
  606. pinfo->gpios = kmalloc_array(ngpios, sizeof(*pinfo->gpios),
  607. GFP_KERNEL);
  608. if (!pinfo->gpios)
  609. return -ENOMEM;
  610. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  611. pinfo->alow_flags = kcalloc(ngpios, sizeof(*pinfo->alow_flags),
  612. GFP_KERNEL);
  613. if (!pinfo->alow_flags) {
  614. ret = -ENOMEM;
  615. goto err_alloc_flags;
  616. }
  617. for (; i < ngpios; i++) {
  618. int gpio;
  619. enum of_gpio_flags flags;
  620. gpio = of_get_gpio_flags(np, i, &flags);
  621. if (!gpio_is_valid(gpio)) {
  622. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  623. ret = gpio;
  624. goto err_loop;
  625. }
  626. ret = gpio_request(gpio, dev_name(dev));
  627. if (ret) {
  628. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  629. goto err_loop;
  630. }
  631. pinfo->gpios[i] = gpio;
  632. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  633. ret = gpio_direction_output(pinfo->gpios[i],
  634. pinfo->alow_flags[i]);
  635. if (ret) {
  636. dev_err(dev,
  637. "can't set output direction for gpio #%d: %d\n",
  638. i, ret);
  639. goto err_loop;
  640. }
  641. }
  642. pdata->max_chipselect = ngpios;
  643. pdata->cs_control = fsl_spi_cs_control;
  644. return 0;
  645. err_loop:
  646. while (i >= 0) {
  647. if (gpio_is_valid(pinfo->gpios[i]))
  648. gpio_free(pinfo->gpios[i]);
  649. i--;
  650. }
  651. kfree(pinfo->alow_flags);
  652. pinfo->alow_flags = NULL;
  653. err_alloc_flags:
  654. kfree(pinfo->gpios);
  655. pinfo->gpios = NULL;
  656. return ret;
  657. }
  658. static int of_fsl_spi_free_chipselects(struct device *dev)
  659. {
  660. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  661. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  662. int i;
  663. if (!pinfo->gpios)
  664. return 0;
  665. for (i = 0; i < pdata->max_chipselect; i++) {
  666. if (gpio_is_valid(pinfo->gpios[i]))
  667. gpio_free(pinfo->gpios[i]);
  668. }
  669. kfree(pinfo->gpios);
  670. kfree(pinfo->alow_flags);
  671. return 0;
  672. }
  673. static int of_fsl_spi_probe(struct platform_device *ofdev)
  674. {
  675. struct device *dev = &ofdev->dev;
  676. struct device_node *np = ofdev->dev.of_node;
  677. struct spi_master *master;
  678. struct resource mem;
  679. int irq = 0, type;
  680. int ret = -ENOMEM;
  681. ret = of_mpc8xxx_spi_probe(ofdev);
  682. if (ret)
  683. return ret;
  684. type = fsl_spi_get_type(&ofdev->dev);
  685. if (type == TYPE_FSL) {
  686. ret = of_fsl_spi_get_chipselects(dev);
  687. if (ret)
  688. goto err;
  689. }
  690. ret = of_address_to_resource(np, 0, &mem);
  691. if (ret)
  692. goto err;
  693. irq = platform_get_irq(ofdev, 0);
  694. if (irq < 0) {
  695. ret = irq;
  696. goto err;
  697. }
  698. master = fsl_spi_probe(dev, &mem, irq);
  699. if (IS_ERR(master)) {
  700. ret = PTR_ERR(master);
  701. goto err;
  702. }
  703. return 0;
  704. err:
  705. if (type == TYPE_FSL)
  706. of_fsl_spi_free_chipselects(dev);
  707. return ret;
  708. }
  709. static int of_fsl_spi_remove(struct platform_device *ofdev)
  710. {
  711. struct spi_master *master = platform_get_drvdata(ofdev);
  712. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
  713. fsl_spi_cpm_free(mpc8xxx_spi);
  714. if (mpc8xxx_spi->type == TYPE_FSL)
  715. of_fsl_spi_free_chipselects(&ofdev->dev);
  716. return 0;
  717. }
  718. static struct platform_driver of_fsl_spi_driver = {
  719. .driver = {
  720. .name = "fsl_spi",
  721. .of_match_table = of_fsl_spi_match,
  722. },
  723. .probe = of_fsl_spi_probe,
  724. .remove = of_fsl_spi_remove,
  725. };
  726. #ifdef CONFIG_MPC832x_RDB
  727. /*
  728. * XXX XXX XXX
  729. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  730. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  731. * tree can work with OpenFirmware driver. But for now we support old trees
  732. * as well.
  733. */
  734. static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  735. {
  736. struct resource *mem;
  737. int irq;
  738. struct spi_master *master;
  739. if (!dev_get_platdata(&pdev->dev))
  740. return -EINVAL;
  741. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  742. if (!mem)
  743. return -EINVAL;
  744. irq = platform_get_irq(pdev, 0);
  745. if (irq <= 0)
  746. return -EINVAL;
  747. master = fsl_spi_probe(&pdev->dev, mem, irq);
  748. return PTR_ERR_OR_ZERO(master);
  749. }
  750. static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  751. {
  752. struct spi_master *master = platform_get_drvdata(pdev);
  753. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
  754. fsl_spi_cpm_free(mpc8xxx_spi);
  755. return 0;
  756. }
  757. MODULE_ALIAS("platform:mpc8xxx_spi");
  758. static struct platform_driver mpc8xxx_spi_driver = {
  759. .probe = plat_mpc8xxx_spi_probe,
  760. .remove = plat_mpc8xxx_spi_remove,
  761. .driver = {
  762. .name = "mpc8xxx_spi",
  763. },
  764. };
  765. static bool legacy_driver_failed;
  766. static void __init legacy_driver_register(void)
  767. {
  768. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  769. }
  770. static void __exit legacy_driver_unregister(void)
  771. {
  772. if (legacy_driver_failed)
  773. return;
  774. platform_driver_unregister(&mpc8xxx_spi_driver);
  775. }
  776. #else
  777. static void __init legacy_driver_register(void) {}
  778. static void __exit legacy_driver_unregister(void) {}
  779. #endif /* CONFIG_MPC832x_RDB */
  780. static int __init fsl_spi_init(void)
  781. {
  782. legacy_driver_register();
  783. return platform_driver_register(&of_fsl_spi_driver);
  784. }
  785. module_init(fsl_spi_init);
  786. static void __exit fsl_spi_exit(void)
  787. {
  788. platform_driver_unregister(&of_fsl_spi_driver);
  789. legacy_driver_unregister();
  790. }
  791. module_exit(fsl_spi_exit);
  792. MODULE_AUTHOR("Kumar Gala");
  793. MODULE_DESCRIPTION("Simple Freescale SPI Driver");
  794. MODULE_LICENSE("GPL");