spi-pxa2xx.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. /*
  2. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  3. * Copyright (C) 2013, Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/ioport.h>
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/spi/pxa2xx_spi.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/delay.h>
  29. #include <linux/gpio.h>
  30. #include <linux/gpio/consumer.h>
  31. #include <linux/slab.h>
  32. #include <linux/clk.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/acpi.h>
  35. #include "spi-pxa2xx.h"
  36. MODULE_AUTHOR("Stephen Street");
  37. MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
  38. MODULE_LICENSE("GPL");
  39. MODULE_ALIAS("platform:pxa2xx-spi");
  40. #define TIMOUT_DFLT 1000
  41. /*
  42. * for testing SSCR1 changes that require SSP restart, basically
  43. * everything except the service and interrupt enables, the pxa270 developer
  44. * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
  45. * list, but the PXA255 dev man says all bits without really meaning the
  46. * service and interrupt enables
  47. */
  48. #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  49. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  50. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  51. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  52. | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
  53. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  54. #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
  55. | QUARK_X1000_SSCR1_EFWR \
  56. | QUARK_X1000_SSCR1_RFT \
  57. | QUARK_X1000_SSCR1_TFT \
  58. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  59. #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  60. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  61. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  62. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  63. | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
  64. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  65. #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
  66. #define LPSS_CS_CONTROL_SW_MODE BIT(0)
  67. #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
  68. #define LPSS_CAPS_CS_EN_SHIFT 9
  69. #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
  70. struct lpss_config {
  71. /* LPSS offset from drv_data->ioaddr */
  72. unsigned offset;
  73. /* Register offsets from drv_data->lpss_base or -1 */
  74. int reg_general;
  75. int reg_ssp;
  76. int reg_cs_ctrl;
  77. int reg_capabilities;
  78. /* FIFO thresholds */
  79. u32 rx_threshold;
  80. u32 tx_threshold_lo;
  81. u32 tx_threshold_hi;
  82. /* Chip select control */
  83. unsigned cs_sel_shift;
  84. unsigned cs_sel_mask;
  85. unsigned cs_num;
  86. };
  87. /* Keep these sorted with enum pxa_ssp_type */
  88. static const struct lpss_config lpss_platforms[] = {
  89. { /* LPSS_LPT_SSP */
  90. .offset = 0x800,
  91. .reg_general = 0x08,
  92. .reg_ssp = 0x0c,
  93. .reg_cs_ctrl = 0x18,
  94. .reg_capabilities = -1,
  95. .rx_threshold = 64,
  96. .tx_threshold_lo = 160,
  97. .tx_threshold_hi = 224,
  98. },
  99. { /* LPSS_BYT_SSP */
  100. .offset = 0x400,
  101. .reg_general = 0x08,
  102. .reg_ssp = 0x0c,
  103. .reg_cs_ctrl = 0x18,
  104. .reg_capabilities = -1,
  105. .rx_threshold = 64,
  106. .tx_threshold_lo = 160,
  107. .tx_threshold_hi = 224,
  108. },
  109. { /* LPSS_BSW_SSP */
  110. .offset = 0x400,
  111. .reg_general = 0x08,
  112. .reg_ssp = 0x0c,
  113. .reg_cs_ctrl = 0x18,
  114. .reg_capabilities = -1,
  115. .rx_threshold = 64,
  116. .tx_threshold_lo = 160,
  117. .tx_threshold_hi = 224,
  118. .cs_sel_shift = 2,
  119. .cs_sel_mask = 1 << 2,
  120. .cs_num = 2,
  121. },
  122. { /* LPSS_SPT_SSP */
  123. .offset = 0x200,
  124. .reg_general = -1,
  125. .reg_ssp = 0x20,
  126. .reg_cs_ctrl = 0x24,
  127. .reg_capabilities = -1,
  128. .rx_threshold = 1,
  129. .tx_threshold_lo = 32,
  130. .tx_threshold_hi = 56,
  131. },
  132. { /* LPSS_BXT_SSP */
  133. .offset = 0x200,
  134. .reg_general = -1,
  135. .reg_ssp = 0x20,
  136. .reg_cs_ctrl = 0x24,
  137. .reg_capabilities = 0xfc,
  138. .rx_threshold = 1,
  139. .tx_threshold_lo = 16,
  140. .tx_threshold_hi = 48,
  141. .cs_sel_shift = 8,
  142. .cs_sel_mask = 3 << 8,
  143. },
  144. { /* LPSS_CNL_SSP */
  145. .offset = 0x200,
  146. .reg_general = -1,
  147. .reg_ssp = 0x20,
  148. .reg_cs_ctrl = 0x24,
  149. .reg_capabilities = 0xfc,
  150. .rx_threshold = 1,
  151. .tx_threshold_lo = 32,
  152. .tx_threshold_hi = 56,
  153. .cs_sel_shift = 8,
  154. .cs_sel_mask = 3 << 8,
  155. },
  156. };
  157. static inline const struct lpss_config
  158. *lpss_get_config(const struct driver_data *drv_data)
  159. {
  160. return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
  161. }
  162. static bool is_lpss_ssp(const struct driver_data *drv_data)
  163. {
  164. switch (drv_data->ssp_type) {
  165. case LPSS_LPT_SSP:
  166. case LPSS_BYT_SSP:
  167. case LPSS_BSW_SSP:
  168. case LPSS_SPT_SSP:
  169. case LPSS_BXT_SSP:
  170. case LPSS_CNL_SSP:
  171. return true;
  172. default:
  173. return false;
  174. }
  175. }
  176. static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
  177. {
  178. return drv_data->ssp_type == QUARK_X1000_SSP;
  179. }
  180. static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
  181. {
  182. switch (drv_data->ssp_type) {
  183. case QUARK_X1000_SSP:
  184. return QUARK_X1000_SSCR1_CHANGE_MASK;
  185. case CE4100_SSP:
  186. return CE4100_SSCR1_CHANGE_MASK;
  187. default:
  188. return SSCR1_CHANGE_MASK;
  189. }
  190. }
  191. static u32
  192. pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
  193. {
  194. switch (drv_data->ssp_type) {
  195. case QUARK_X1000_SSP:
  196. return RX_THRESH_QUARK_X1000_DFLT;
  197. case CE4100_SSP:
  198. return RX_THRESH_CE4100_DFLT;
  199. default:
  200. return RX_THRESH_DFLT;
  201. }
  202. }
  203. static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
  204. {
  205. u32 mask;
  206. switch (drv_data->ssp_type) {
  207. case QUARK_X1000_SSP:
  208. mask = QUARK_X1000_SSSR_TFL_MASK;
  209. break;
  210. case CE4100_SSP:
  211. mask = CE4100_SSSR_TFL_MASK;
  212. break;
  213. default:
  214. mask = SSSR_TFL_MASK;
  215. break;
  216. }
  217. return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
  218. }
  219. static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
  220. u32 *sccr1_reg)
  221. {
  222. u32 mask;
  223. switch (drv_data->ssp_type) {
  224. case QUARK_X1000_SSP:
  225. mask = QUARK_X1000_SSCR1_RFT;
  226. break;
  227. case CE4100_SSP:
  228. mask = CE4100_SSCR1_RFT;
  229. break;
  230. default:
  231. mask = SSCR1_RFT;
  232. break;
  233. }
  234. *sccr1_reg &= ~mask;
  235. }
  236. static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
  237. u32 *sccr1_reg, u32 threshold)
  238. {
  239. switch (drv_data->ssp_type) {
  240. case QUARK_X1000_SSP:
  241. *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
  242. break;
  243. case CE4100_SSP:
  244. *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
  245. break;
  246. default:
  247. *sccr1_reg |= SSCR1_RxTresh(threshold);
  248. break;
  249. }
  250. }
  251. static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
  252. u32 clk_div, u8 bits)
  253. {
  254. switch (drv_data->ssp_type) {
  255. case QUARK_X1000_SSP:
  256. return clk_div
  257. | QUARK_X1000_SSCR0_Motorola
  258. | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
  259. | SSCR0_SSE;
  260. default:
  261. return clk_div
  262. | SSCR0_Motorola
  263. | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
  264. | SSCR0_SSE
  265. | (bits > 16 ? SSCR0_EDSS : 0);
  266. }
  267. }
  268. /*
  269. * Read and write LPSS SSP private registers. Caller must first check that
  270. * is_lpss_ssp() returns true before these can be called.
  271. */
  272. static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
  273. {
  274. WARN_ON(!drv_data->lpss_base);
  275. return readl(drv_data->lpss_base + offset);
  276. }
  277. static void __lpss_ssp_write_priv(struct driver_data *drv_data,
  278. unsigned offset, u32 value)
  279. {
  280. WARN_ON(!drv_data->lpss_base);
  281. writel(value, drv_data->lpss_base + offset);
  282. }
  283. /*
  284. * lpss_ssp_setup - perform LPSS SSP specific setup
  285. * @drv_data: pointer to the driver private data
  286. *
  287. * Perform LPSS SSP specific setup. This function must be called first if
  288. * one is going to use LPSS SSP private registers.
  289. */
  290. static void lpss_ssp_setup(struct driver_data *drv_data)
  291. {
  292. const struct lpss_config *config;
  293. u32 value;
  294. config = lpss_get_config(drv_data);
  295. drv_data->lpss_base = drv_data->ioaddr + config->offset;
  296. /* Enable software chip select control */
  297. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  298. value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
  299. value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
  300. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  301. /* Enable multiblock DMA transfers */
  302. if (drv_data->master_info->enable_dma) {
  303. __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
  304. if (config->reg_general >= 0) {
  305. value = __lpss_ssp_read_priv(drv_data,
  306. config->reg_general);
  307. value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
  308. __lpss_ssp_write_priv(drv_data,
  309. config->reg_general, value);
  310. }
  311. }
  312. }
  313. static void lpss_ssp_select_cs(struct driver_data *drv_data,
  314. const struct lpss_config *config)
  315. {
  316. u32 value, cs;
  317. if (!config->cs_sel_mask)
  318. return;
  319. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  320. cs = drv_data->master->cur_msg->spi->chip_select;
  321. cs <<= config->cs_sel_shift;
  322. if (cs != (value & config->cs_sel_mask)) {
  323. /*
  324. * When switching another chip select output active the
  325. * output must be selected first and wait 2 ssp_clk cycles
  326. * before changing state to active. Otherwise a short
  327. * glitch will occur on the previous chip select since
  328. * output select is latched but state control is not.
  329. */
  330. value &= ~config->cs_sel_mask;
  331. value |= cs;
  332. __lpss_ssp_write_priv(drv_data,
  333. config->reg_cs_ctrl, value);
  334. ndelay(1000000000 /
  335. (drv_data->master->max_speed_hz / 2));
  336. }
  337. }
  338. static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
  339. {
  340. const struct lpss_config *config;
  341. u32 value;
  342. config = lpss_get_config(drv_data);
  343. if (enable)
  344. lpss_ssp_select_cs(drv_data, config);
  345. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  346. if (enable)
  347. value &= ~LPSS_CS_CONTROL_CS_HIGH;
  348. else
  349. value |= LPSS_CS_CONTROL_CS_HIGH;
  350. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  351. }
  352. static void cs_assert(struct driver_data *drv_data)
  353. {
  354. struct chip_data *chip =
  355. spi_get_ctldata(drv_data->master->cur_msg->spi);
  356. if (drv_data->ssp_type == CE4100_SSP) {
  357. pxa2xx_spi_write(drv_data, SSSR, chip->frm);
  358. return;
  359. }
  360. if (chip->cs_control) {
  361. chip->cs_control(PXA2XX_CS_ASSERT);
  362. return;
  363. }
  364. if (chip->gpiod_cs) {
  365. gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
  366. return;
  367. }
  368. if (is_lpss_ssp(drv_data))
  369. lpss_ssp_cs_control(drv_data, true);
  370. }
  371. static void cs_deassert(struct driver_data *drv_data)
  372. {
  373. struct chip_data *chip =
  374. spi_get_ctldata(drv_data->master->cur_msg->spi);
  375. if (drv_data->ssp_type == CE4100_SSP)
  376. return;
  377. if (chip->cs_control) {
  378. chip->cs_control(PXA2XX_CS_DEASSERT);
  379. return;
  380. }
  381. if (chip->gpiod_cs) {
  382. gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
  383. return;
  384. }
  385. if (is_lpss_ssp(drv_data))
  386. lpss_ssp_cs_control(drv_data, false);
  387. }
  388. int pxa2xx_spi_flush(struct driver_data *drv_data)
  389. {
  390. unsigned long limit = loops_per_jiffy << 1;
  391. do {
  392. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  393. pxa2xx_spi_read(drv_data, SSDR);
  394. } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
  395. write_SSSR_CS(drv_data, SSSR_ROR);
  396. return limit;
  397. }
  398. static int null_writer(struct driver_data *drv_data)
  399. {
  400. u8 n_bytes = drv_data->n_bytes;
  401. if (pxa2xx_spi_txfifo_full(drv_data)
  402. || (drv_data->tx == drv_data->tx_end))
  403. return 0;
  404. pxa2xx_spi_write(drv_data, SSDR, 0);
  405. drv_data->tx += n_bytes;
  406. return 1;
  407. }
  408. static int null_reader(struct driver_data *drv_data)
  409. {
  410. u8 n_bytes = drv_data->n_bytes;
  411. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  412. && (drv_data->rx < drv_data->rx_end)) {
  413. pxa2xx_spi_read(drv_data, SSDR);
  414. drv_data->rx += n_bytes;
  415. }
  416. return drv_data->rx == drv_data->rx_end;
  417. }
  418. static int u8_writer(struct driver_data *drv_data)
  419. {
  420. if (pxa2xx_spi_txfifo_full(drv_data)
  421. || (drv_data->tx == drv_data->tx_end))
  422. return 0;
  423. pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
  424. ++drv_data->tx;
  425. return 1;
  426. }
  427. static int u8_reader(struct driver_data *drv_data)
  428. {
  429. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  430. && (drv_data->rx < drv_data->rx_end)) {
  431. *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  432. ++drv_data->rx;
  433. }
  434. return drv_data->rx == drv_data->rx_end;
  435. }
  436. static int u16_writer(struct driver_data *drv_data)
  437. {
  438. if (pxa2xx_spi_txfifo_full(drv_data)
  439. || (drv_data->tx == drv_data->tx_end))
  440. return 0;
  441. pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
  442. drv_data->tx += 2;
  443. return 1;
  444. }
  445. static int u16_reader(struct driver_data *drv_data)
  446. {
  447. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  448. && (drv_data->rx < drv_data->rx_end)) {
  449. *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  450. drv_data->rx += 2;
  451. }
  452. return drv_data->rx == drv_data->rx_end;
  453. }
  454. static int u32_writer(struct driver_data *drv_data)
  455. {
  456. if (pxa2xx_spi_txfifo_full(drv_data)
  457. || (drv_data->tx == drv_data->tx_end))
  458. return 0;
  459. pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
  460. drv_data->tx += 4;
  461. return 1;
  462. }
  463. static int u32_reader(struct driver_data *drv_data)
  464. {
  465. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  466. && (drv_data->rx < drv_data->rx_end)) {
  467. *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  468. drv_data->rx += 4;
  469. }
  470. return drv_data->rx == drv_data->rx_end;
  471. }
  472. void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
  473. {
  474. struct spi_message *msg = drv_data->master->cur_msg;
  475. struct spi_transfer *trans = drv_data->cur_transfer;
  476. /* Move to next transfer */
  477. if (trans->transfer_list.next != &msg->transfers) {
  478. drv_data->cur_transfer =
  479. list_entry(trans->transfer_list.next,
  480. struct spi_transfer,
  481. transfer_list);
  482. return RUNNING_STATE;
  483. } else
  484. return DONE_STATE;
  485. }
  486. /* caller already set message->status; dma and pio irqs are blocked */
  487. static void giveback(struct driver_data *drv_data)
  488. {
  489. struct spi_transfer* last_transfer;
  490. struct spi_message *msg;
  491. unsigned long timeout;
  492. msg = drv_data->master->cur_msg;
  493. drv_data->cur_transfer = NULL;
  494. last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
  495. transfer_list);
  496. /* Delay if requested before any change in chip select */
  497. if (last_transfer->delay_usecs)
  498. udelay(last_transfer->delay_usecs);
  499. /* Wait until SSP becomes idle before deasserting the CS */
  500. timeout = jiffies + msecs_to_jiffies(10);
  501. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
  502. !time_after(jiffies, timeout))
  503. cpu_relax();
  504. /* Drop chip select UNLESS cs_change is true or we are returning
  505. * a message with an error, or next message is for another chip
  506. */
  507. if (!last_transfer->cs_change)
  508. cs_deassert(drv_data);
  509. else {
  510. struct spi_message *next_msg;
  511. /* Holding of cs was hinted, but we need to make sure
  512. * the next message is for the same chip. Don't waste
  513. * time with the following tests unless this was hinted.
  514. *
  515. * We cannot postpone this until pump_messages, because
  516. * after calling msg->complete (below) the driver that
  517. * sent the current message could be unloaded, which
  518. * could invalidate the cs_control() callback...
  519. */
  520. /* get a pointer to the next message, if any */
  521. next_msg = spi_get_next_queued_message(drv_data->master);
  522. /* see if the next and current messages point
  523. * to the same chip
  524. */
  525. if ((next_msg && next_msg->spi != msg->spi) ||
  526. msg->state == ERROR_STATE)
  527. cs_deassert(drv_data);
  528. }
  529. spi_finalize_current_message(drv_data->master);
  530. }
  531. static void reset_sccr1(struct driver_data *drv_data)
  532. {
  533. struct chip_data *chip =
  534. spi_get_ctldata(drv_data->master->cur_msg->spi);
  535. u32 sccr1_reg;
  536. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
  537. switch (drv_data->ssp_type) {
  538. case QUARK_X1000_SSP:
  539. sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
  540. break;
  541. case CE4100_SSP:
  542. sccr1_reg &= ~CE4100_SSCR1_RFT;
  543. break;
  544. default:
  545. sccr1_reg &= ~SSCR1_RFT;
  546. break;
  547. }
  548. sccr1_reg |= chip->threshold;
  549. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  550. }
  551. static void int_error_stop(struct driver_data *drv_data, const char* msg)
  552. {
  553. /* Stop and reset SSP */
  554. write_SSSR_CS(drv_data, drv_data->clear_sr);
  555. reset_sccr1(drv_data);
  556. if (!pxa25x_ssp_comp(drv_data))
  557. pxa2xx_spi_write(drv_data, SSTO, 0);
  558. pxa2xx_spi_flush(drv_data);
  559. pxa2xx_spi_write(drv_data, SSCR0,
  560. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  561. dev_err(&drv_data->pdev->dev, "%s\n", msg);
  562. drv_data->master->cur_msg->state = ERROR_STATE;
  563. tasklet_schedule(&drv_data->pump_transfers);
  564. }
  565. static void int_transfer_complete(struct driver_data *drv_data)
  566. {
  567. /* Clear and disable interrupts */
  568. write_SSSR_CS(drv_data, drv_data->clear_sr);
  569. reset_sccr1(drv_data);
  570. if (!pxa25x_ssp_comp(drv_data))
  571. pxa2xx_spi_write(drv_data, SSTO, 0);
  572. /* Update total byte transferred return count actual bytes read */
  573. drv_data->master->cur_msg->actual_length += drv_data->len -
  574. (drv_data->rx_end - drv_data->rx);
  575. /* Transfer delays and chip select release are
  576. * handled in pump_transfers or giveback
  577. */
  578. /* Move to next transfer */
  579. drv_data->master->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
  580. /* Schedule transfer tasklet */
  581. tasklet_schedule(&drv_data->pump_transfers);
  582. }
  583. static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
  584. {
  585. u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
  586. drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
  587. u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
  588. if (irq_status & SSSR_ROR) {
  589. int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
  590. return IRQ_HANDLED;
  591. }
  592. if (irq_status & SSSR_TINT) {
  593. pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
  594. if (drv_data->read(drv_data)) {
  595. int_transfer_complete(drv_data);
  596. return IRQ_HANDLED;
  597. }
  598. }
  599. /* Drain rx fifo, Fill tx fifo and prevent overruns */
  600. do {
  601. if (drv_data->read(drv_data)) {
  602. int_transfer_complete(drv_data);
  603. return IRQ_HANDLED;
  604. }
  605. } while (drv_data->write(drv_data));
  606. if (drv_data->read(drv_data)) {
  607. int_transfer_complete(drv_data);
  608. return IRQ_HANDLED;
  609. }
  610. if (drv_data->tx == drv_data->tx_end) {
  611. u32 bytes_left;
  612. u32 sccr1_reg;
  613. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  614. sccr1_reg &= ~SSCR1_TIE;
  615. /*
  616. * PXA25x_SSP has no timeout, set up rx threshould for the
  617. * remaining RX bytes.
  618. */
  619. if (pxa25x_ssp_comp(drv_data)) {
  620. u32 rx_thre;
  621. pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
  622. bytes_left = drv_data->rx_end - drv_data->rx;
  623. switch (drv_data->n_bytes) {
  624. case 4:
  625. bytes_left >>= 1;
  626. case 2:
  627. bytes_left >>= 1;
  628. }
  629. rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
  630. if (rx_thre > bytes_left)
  631. rx_thre = bytes_left;
  632. pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
  633. }
  634. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  635. }
  636. /* We did something */
  637. return IRQ_HANDLED;
  638. }
  639. static void handle_bad_msg(struct driver_data *drv_data)
  640. {
  641. pxa2xx_spi_write(drv_data, SSCR0,
  642. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  643. pxa2xx_spi_write(drv_data, SSCR1,
  644. pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
  645. if (!pxa25x_ssp_comp(drv_data))
  646. pxa2xx_spi_write(drv_data, SSTO, 0);
  647. write_SSSR_CS(drv_data, drv_data->clear_sr);
  648. dev_err(&drv_data->pdev->dev,
  649. "bad message state in interrupt handler\n");
  650. }
  651. static irqreturn_t ssp_int(int irq, void *dev_id)
  652. {
  653. struct driver_data *drv_data = dev_id;
  654. u32 sccr1_reg;
  655. u32 mask = drv_data->mask_sr;
  656. u32 status;
  657. /*
  658. * The IRQ might be shared with other peripherals so we must first
  659. * check that are we RPM suspended or not. If we are we assume that
  660. * the IRQ was not for us (we shouldn't be RPM suspended when the
  661. * interrupt is enabled).
  662. */
  663. if (pm_runtime_suspended(&drv_data->pdev->dev))
  664. return IRQ_NONE;
  665. /*
  666. * If the device is not yet in RPM suspended state and we get an
  667. * interrupt that is meant for another device, check if status bits
  668. * are all set to one. That means that the device is already
  669. * powered off.
  670. */
  671. status = pxa2xx_spi_read(drv_data, SSSR);
  672. if (status == ~0)
  673. return IRQ_NONE;
  674. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  675. /* Ignore possible writes if we don't need to write */
  676. if (!(sccr1_reg & SSCR1_TIE))
  677. mask &= ~SSSR_TFS;
  678. /* Ignore RX timeout interrupt if it is disabled */
  679. if (!(sccr1_reg & SSCR1_TINTE))
  680. mask &= ~SSSR_TINT;
  681. if (!(status & mask))
  682. return IRQ_NONE;
  683. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
  684. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  685. if (!drv_data->master->cur_msg) {
  686. handle_bad_msg(drv_data);
  687. /* Never fail */
  688. return IRQ_HANDLED;
  689. }
  690. return drv_data->transfer_handler(drv_data);
  691. }
  692. /*
  693. * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
  694. * input frequency by fractions of 2^24. It also has a divider by 5.
  695. *
  696. * There are formulas to get baud rate value for given input frequency and
  697. * divider parameters, such as DDS_CLK_RATE and SCR:
  698. *
  699. * Fsys = 200MHz
  700. *
  701. * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
  702. * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
  703. *
  704. * DDS_CLK_RATE either 2^n or 2^n / 5.
  705. * SCR is in range 0 .. 255
  706. *
  707. * Divisor = 5^i * 2^j * 2 * k
  708. * i = [0, 1] i = 1 iff j = 0 or j > 3
  709. * j = [0, 23] j = 0 iff i = 1
  710. * k = [1, 256]
  711. * Special case: j = 0, i = 1: Divisor = 2 / 5
  712. *
  713. * Accordingly to the specification the recommended values for DDS_CLK_RATE
  714. * are:
  715. * Case 1: 2^n, n = [0, 23]
  716. * Case 2: 2^24 * 2 / 5 (0x666666)
  717. * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
  718. *
  719. * In all cases the lowest possible value is better.
  720. *
  721. * The function calculates parameters for all cases and chooses the one closest
  722. * to the asked baud rate.
  723. */
  724. static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
  725. {
  726. unsigned long xtal = 200000000;
  727. unsigned long fref = xtal / 2; /* mandatory division by 2,
  728. see (2) */
  729. /* case 3 */
  730. unsigned long fref1 = fref / 2; /* case 1 */
  731. unsigned long fref2 = fref * 2 / 5; /* case 2 */
  732. unsigned long scale;
  733. unsigned long q, q1, q2;
  734. long r, r1, r2;
  735. u32 mul;
  736. /* Case 1 */
  737. /* Set initial value for DDS_CLK_RATE */
  738. mul = (1 << 24) >> 1;
  739. /* Calculate initial quot */
  740. q1 = DIV_ROUND_UP(fref1, rate);
  741. /* Scale q1 if it's too big */
  742. if (q1 > 256) {
  743. /* Scale q1 to range [1, 512] */
  744. scale = fls_long(q1 - 1);
  745. if (scale > 9) {
  746. q1 >>= scale - 9;
  747. mul >>= scale - 9;
  748. }
  749. /* Round the result if we have a remainder */
  750. q1 += q1 & 1;
  751. }
  752. /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
  753. scale = __ffs(q1);
  754. q1 >>= scale;
  755. mul >>= scale;
  756. /* Get the remainder */
  757. r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
  758. /* Case 2 */
  759. q2 = DIV_ROUND_UP(fref2, rate);
  760. r2 = abs(fref2 / q2 - rate);
  761. /*
  762. * Choose the best between two: less remainder we have the better. We
  763. * can't go case 2 if q2 is greater than 256 since SCR register can
  764. * hold only values 0 .. 255.
  765. */
  766. if (r2 >= r1 || q2 > 256) {
  767. /* case 1 is better */
  768. r = r1;
  769. q = q1;
  770. } else {
  771. /* case 2 is better */
  772. r = r2;
  773. q = q2;
  774. mul = (1 << 24) * 2 / 5;
  775. }
  776. /* Check case 3 only if the divisor is big enough */
  777. if (fref / rate >= 80) {
  778. u64 fssp;
  779. u32 m;
  780. /* Calculate initial quot */
  781. q1 = DIV_ROUND_UP(fref, rate);
  782. m = (1 << 24) / q1;
  783. /* Get the remainder */
  784. fssp = (u64)fref * m;
  785. do_div(fssp, 1 << 24);
  786. r1 = abs(fssp - rate);
  787. /* Choose this one if it suits better */
  788. if (r1 < r) {
  789. /* case 3 is better */
  790. q = 1;
  791. mul = m;
  792. }
  793. }
  794. *dds = mul;
  795. return q - 1;
  796. }
  797. static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
  798. {
  799. unsigned long ssp_clk = drv_data->master->max_speed_hz;
  800. const struct ssp_device *ssp = drv_data->ssp;
  801. rate = min_t(int, ssp_clk, rate);
  802. if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
  803. return (ssp_clk / (2 * rate) - 1) & 0xff;
  804. else
  805. return (ssp_clk / rate - 1) & 0xfff;
  806. }
  807. static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
  808. int rate)
  809. {
  810. struct chip_data *chip =
  811. spi_get_ctldata(drv_data->master->cur_msg->spi);
  812. unsigned int clk_div;
  813. switch (drv_data->ssp_type) {
  814. case QUARK_X1000_SSP:
  815. clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
  816. break;
  817. default:
  818. clk_div = ssp_get_clk_div(drv_data, rate);
  819. break;
  820. }
  821. return clk_div << 8;
  822. }
  823. static bool pxa2xx_spi_can_dma(struct spi_master *master,
  824. struct spi_device *spi,
  825. struct spi_transfer *xfer)
  826. {
  827. struct chip_data *chip = spi_get_ctldata(spi);
  828. return chip->enable_dma &&
  829. xfer->len <= MAX_DMA_LEN &&
  830. xfer->len >= chip->dma_burst_size;
  831. }
  832. static void pump_transfers(unsigned long data)
  833. {
  834. struct driver_data *drv_data = (struct driver_data *)data;
  835. struct spi_master *master = drv_data->master;
  836. struct spi_message *message = master->cur_msg;
  837. struct chip_data *chip = spi_get_ctldata(message->spi);
  838. u32 dma_thresh = chip->dma_threshold;
  839. u32 dma_burst = chip->dma_burst_size;
  840. u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
  841. struct spi_transfer *transfer;
  842. struct spi_transfer *previous;
  843. u32 clk_div;
  844. u8 bits;
  845. u32 speed;
  846. u32 cr0;
  847. u32 cr1;
  848. int err;
  849. int dma_mapped;
  850. /* Get current state information */
  851. transfer = drv_data->cur_transfer;
  852. /* Handle for abort */
  853. if (message->state == ERROR_STATE) {
  854. message->status = -EIO;
  855. giveback(drv_data);
  856. return;
  857. }
  858. /* Handle end of message */
  859. if (message->state == DONE_STATE) {
  860. message->status = 0;
  861. giveback(drv_data);
  862. return;
  863. }
  864. /* Delay if requested at end of transfer before CS change */
  865. if (message->state == RUNNING_STATE) {
  866. previous = list_entry(transfer->transfer_list.prev,
  867. struct spi_transfer,
  868. transfer_list);
  869. if (previous->delay_usecs)
  870. udelay(previous->delay_usecs);
  871. /* Drop chip select only if cs_change is requested */
  872. if (previous->cs_change)
  873. cs_deassert(drv_data);
  874. }
  875. /* Check if we can DMA this transfer */
  876. if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
  877. /* reject already-mapped transfers; PIO won't always work */
  878. if (message->is_dma_mapped
  879. || transfer->rx_dma || transfer->tx_dma) {
  880. dev_err(&drv_data->pdev->dev,
  881. "pump_transfers: mapped transfer length of "
  882. "%u is greater than %d\n",
  883. transfer->len, MAX_DMA_LEN);
  884. message->status = -EINVAL;
  885. giveback(drv_data);
  886. return;
  887. }
  888. /* warn ... we force this to PIO mode */
  889. dev_warn_ratelimited(&message->spi->dev,
  890. "pump_transfers: DMA disabled for transfer length %ld "
  891. "greater than %d\n",
  892. (long)drv_data->len, MAX_DMA_LEN);
  893. }
  894. /* Setup the transfer state based on the type of transfer */
  895. if (pxa2xx_spi_flush(drv_data) == 0) {
  896. dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
  897. message->status = -EIO;
  898. giveback(drv_data);
  899. return;
  900. }
  901. drv_data->n_bytes = chip->n_bytes;
  902. drv_data->tx = (void *)transfer->tx_buf;
  903. drv_data->tx_end = drv_data->tx + transfer->len;
  904. drv_data->rx = transfer->rx_buf;
  905. drv_data->rx_end = drv_data->rx + transfer->len;
  906. drv_data->len = transfer->len;
  907. drv_data->write = drv_data->tx ? chip->write : null_writer;
  908. drv_data->read = drv_data->rx ? chip->read : null_reader;
  909. /* Change speed and bit per word on a per transfer */
  910. bits = transfer->bits_per_word;
  911. speed = transfer->speed_hz;
  912. clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
  913. if (bits <= 8) {
  914. drv_data->n_bytes = 1;
  915. drv_data->read = drv_data->read != null_reader ?
  916. u8_reader : null_reader;
  917. drv_data->write = drv_data->write != null_writer ?
  918. u8_writer : null_writer;
  919. } else if (bits <= 16) {
  920. drv_data->n_bytes = 2;
  921. drv_data->read = drv_data->read != null_reader ?
  922. u16_reader : null_reader;
  923. drv_data->write = drv_data->write != null_writer ?
  924. u16_writer : null_writer;
  925. } else if (bits <= 32) {
  926. drv_data->n_bytes = 4;
  927. drv_data->read = drv_data->read != null_reader ?
  928. u32_reader : null_reader;
  929. drv_data->write = drv_data->write != null_writer ?
  930. u32_writer : null_writer;
  931. }
  932. /*
  933. * if bits/word is changed in dma mode, then must check the
  934. * thresholds and burst also
  935. */
  936. if (chip->enable_dma) {
  937. if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
  938. message->spi,
  939. bits, &dma_burst,
  940. &dma_thresh))
  941. dev_warn_ratelimited(&message->spi->dev,
  942. "pump_transfers: DMA burst size reduced to match bits_per_word\n");
  943. }
  944. message->state = RUNNING_STATE;
  945. dma_mapped = master->can_dma &&
  946. master->can_dma(master, message->spi, transfer) &&
  947. master->cur_msg_mapped;
  948. if (dma_mapped) {
  949. /* Ensure we have the correct interrupt handler */
  950. drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
  951. err = pxa2xx_spi_dma_prepare(drv_data, dma_burst);
  952. if (err) {
  953. message->status = err;
  954. giveback(drv_data);
  955. return;
  956. }
  957. /* Clear status and start DMA engine */
  958. cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
  959. pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
  960. pxa2xx_spi_dma_start(drv_data);
  961. } else {
  962. /* Ensure we have the correct interrupt handler */
  963. drv_data->transfer_handler = interrupt_transfer;
  964. /* Clear status */
  965. cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
  966. write_SSSR_CS(drv_data, drv_data->clear_sr);
  967. }
  968. /* NOTE: PXA25x_SSP _could_ use external clocking ... */
  969. cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
  970. if (!pxa25x_ssp_comp(drv_data))
  971. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  972. master->max_speed_hz
  973. / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
  974. dma_mapped ? "DMA" : "PIO");
  975. else
  976. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  977. master->max_speed_hz / 2
  978. / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
  979. dma_mapped ? "DMA" : "PIO");
  980. if (is_lpss_ssp(drv_data)) {
  981. if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
  982. != chip->lpss_rx_threshold)
  983. pxa2xx_spi_write(drv_data, SSIRF,
  984. chip->lpss_rx_threshold);
  985. if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
  986. != chip->lpss_tx_threshold)
  987. pxa2xx_spi_write(drv_data, SSITF,
  988. chip->lpss_tx_threshold);
  989. }
  990. if (is_quark_x1000_ssp(drv_data) &&
  991. (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
  992. pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
  993. /* see if we need to reload the config registers */
  994. if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
  995. || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
  996. != (cr1 & change_mask)) {
  997. /* stop the SSP, and update the other bits */
  998. pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
  999. if (!pxa25x_ssp_comp(drv_data))
  1000. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  1001. /* first set CR1 without interrupt and service enables */
  1002. pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
  1003. /* restart the SSP */
  1004. pxa2xx_spi_write(drv_data, SSCR0, cr0);
  1005. } else {
  1006. if (!pxa25x_ssp_comp(drv_data))
  1007. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  1008. }
  1009. cs_assert(drv_data);
  1010. /* after chip select, release the data by enabling service
  1011. * requests and interrupts, without changing any mode bits */
  1012. pxa2xx_spi_write(drv_data, SSCR1, cr1);
  1013. }
  1014. static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
  1015. struct spi_message *msg)
  1016. {
  1017. struct driver_data *drv_data = spi_master_get_devdata(master);
  1018. /* Initial message state*/
  1019. msg->state = START_STATE;
  1020. drv_data->cur_transfer = list_entry(msg->transfers.next,
  1021. struct spi_transfer,
  1022. transfer_list);
  1023. /* Mark as busy and launch transfers */
  1024. tasklet_schedule(&drv_data->pump_transfers);
  1025. return 0;
  1026. }
  1027. static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
  1028. {
  1029. struct driver_data *drv_data = spi_master_get_devdata(master);
  1030. /* Disable the SSP now */
  1031. pxa2xx_spi_write(drv_data, SSCR0,
  1032. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  1033. return 0;
  1034. }
  1035. static int setup_cs(struct spi_device *spi, struct chip_data *chip,
  1036. struct pxa2xx_spi_chip *chip_info)
  1037. {
  1038. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1039. struct gpio_desc *gpiod;
  1040. int err = 0;
  1041. if (chip == NULL)
  1042. return 0;
  1043. if (drv_data->cs_gpiods) {
  1044. gpiod = drv_data->cs_gpiods[spi->chip_select];
  1045. if (gpiod) {
  1046. chip->gpiod_cs = gpiod;
  1047. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1048. gpiod_set_value(gpiod, chip->gpio_cs_inverted);
  1049. }
  1050. return 0;
  1051. }
  1052. if (chip_info == NULL)
  1053. return 0;
  1054. /* NOTE: setup() can be called multiple times, possibly with
  1055. * different chip_info, release previously requested GPIO
  1056. */
  1057. if (chip->gpiod_cs) {
  1058. gpio_free(desc_to_gpio(chip->gpiod_cs));
  1059. chip->gpiod_cs = NULL;
  1060. }
  1061. /* If (*cs_control) is provided, ignore GPIO chip select */
  1062. if (chip_info->cs_control) {
  1063. chip->cs_control = chip_info->cs_control;
  1064. return 0;
  1065. }
  1066. if (gpio_is_valid(chip_info->gpio_cs)) {
  1067. err = gpio_request(chip_info->gpio_cs, "SPI_CS");
  1068. if (err) {
  1069. dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
  1070. chip_info->gpio_cs);
  1071. return err;
  1072. }
  1073. gpiod = gpio_to_desc(chip_info->gpio_cs);
  1074. chip->gpiod_cs = gpiod;
  1075. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1076. err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
  1077. }
  1078. return err;
  1079. }
  1080. static int setup(struct spi_device *spi)
  1081. {
  1082. struct pxa2xx_spi_chip *chip_info;
  1083. struct chip_data *chip;
  1084. const struct lpss_config *config;
  1085. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1086. uint tx_thres, tx_hi_thres, rx_thres;
  1087. switch (drv_data->ssp_type) {
  1088. case QUARK_X1000_SSP:
  1089. tx_thres = TX_THRESH_QUARK_X1000_DFLT;
  1090. tx_hi_thres = 0;
  1091. rx_thres = RX_THRESH_QUARK_X1000_DFLT;
  1092. break;
  1093. case CE4100_SSP:
  1094. tx_thres = TX_THRESH_CE4100_DFLT;
  1095. tx_hi_thres = 0;
  1096. rx_thres = RX_THRESH_CE4100_DFLT;
  1097. break;
  1098. case LPSS_LPT_SSP:
  1099. case LPSS_BYT_SSP:
  1100. case LPSS_BSW_SSP:
  1101. case LPSS_SPT_SSP:
  1102. case LPSS_BXT_SSP:
  1103. case LPSS_CNL_SSP:
  1104. config = lpss_get_config(drv_data);
  1105. tx_thres = config->tx_threshold_lo;
  1106. tx_hi_thres = config->tx_threshold_hi;
  1107. rx_thres = config->rx_threshold;
  1108. break;
  1109. default:
  1110. tx_thres = TX_THRESH_DFLT;
  1111. tx_hi_thres = 0;
  1112. rx_thres = RX_THRESH_DFLT;
  1113. break;
  1114. }
  1115. /* Only alloc on first setup */
  1116. chip = spi_get_ctldata(spi);
  1117. if (!chip) {
  1118. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1119. if (!chip)
  1120. return -ENOMEM;
  1121. if (drv_data->ssp_type == CE4100_SSP) {
  1122. if (spi->chip_select > 4) {
  1123. dev_err(&spi->dev,
  1124. "failed setup: cs number must not be > 4.\n");
  1125. kfree(chip);
  1126. return -EINVAL;
  1127. }
  1128. chip->frm = spi->chip_select;
  1129. }
  1130. chip->enable_dma = drv_data->master_info->enable_dma;
  1131. chip->timeout = TIMOUT_DFLT;
  1132. }
  1133. /* protocol drivers may change the chip settings, so...
  1134. * if chip_info exists, use it */
  1135. chip_info = spi->controller_data;
  1136. /* chip_info isn't always needed */
  1137. chip->cr1 = 0;
  1138. if (chip_info) {
  1139. if (chip_info->timeout)
  1140. chip->timeout = chip_info->timeout;
  1141. if (chip_info->tx_threshold)
  1142. tx_thres = chip_info->tx_threshold;
  1143. if (chip_info->tx_hi_threshold)
  1144. tx_hi_thres = chip_info->tx_hi_threshold;
  1145. if (chip_info->rx_threshold)
  1146. rx_thres = chip_info->rx_threshold;
  1147. chip->dma_threshold = 0;
  1148. if (chip_info->enable_loopback)
  1149. chip->cr1 = SSCR1_LBM;
  1150. }
  1151. chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
  1152. chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
  1153. | SSITF_TxHiThresh(tx_hi_thres);
  1154. /* set dma burst and threshold outside of chip_info path so that if
  1155. * chip_info goes away after setting chip->enable_dma, the
  1156. * burst and threshold can still respond to changes in bits_per_word */
  1157. if (chip->enable_dma) {
  1158. /* set up legal burst and threshold for dma */
  1159. if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
  1160. spi->bits_per_word,
  1161. &chip->dma_burst_size,
  1162. &chip->dma_threshold)) {
  1163. dev_warn(&spi->dev,
  1164. "in setup: DMA burst size reduced to match bits_per_word\n");
  1165. }
  1166. }
  1167. switch (drv_data->ssp_type) {
  1168. case QUARK_X1000_SSP:
  1169. chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
  1170. & QUARK_X1000_SSCR1_RFT)
  1171. | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
  1172. & QUARK_X1000_SSCR1_TFT);
  1173. break;
  1174. case CE4100_SSP:
  1175. chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
  1176. (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
  1177. break;
  1178. default:
  1179. chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
  1180. (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
  1181. break;
  1182. }
  1183. chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  1184. chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  1185. | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  1186. if (spi->mode & SPI_LOOP)
  1187. chip->cr1 |= SSCR1_LBM;
  1188. if (spi->bits_per_word <= 8) {
  1189. chip->n_bytes = 1;
  1190. chip->read = u8_reader;
  1191. chip->write = u8_writer;
  1192. } else if (spi->bits_per_word <= 16) {
  1193. chip->n_bytes = 2;
  1194. chip->read = u16_reader;
  1195. chip->write = u16_writer;
  1196. } else if (spi->bits_per_word <= 32) {
  1197. chip->n_bytes = 4;
  1198. chip->read = u32_reader;
  1199. chip->write = u32_writer;
  1200. }
  1201. spi_set_ctldata(spi, chip);
  1202. if (drv_data->ssp_type == CE4100_SSP)
  1203. return 0;
  1204. return setup_cs(spi, chip, chip_info);
  1205. }
  1206. static void cleanup(struct spi_device *spi)
  1207. {
  1208. struct chip_data *chip = spi_get_ctldata(spi);
  1209. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1210. if (!chip)
  1211. return;
  1212. if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
  1213. chip->gpiod_cs)
  1214. gpio_free(desc_to_gpio(chip->gpiod_cs));
  1215. kfree(chip);
  1216. }
  1217. #ifdef CONFIG_PCI
  1218. #ifdef CONFIG_ACPI
  1219. static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
  1220. { "INT33C0", LPSS_LPT_SSP },
  1221. { "INT33C1", LPSS_LPT_SSP },
  1222. { "INT3430", LPSS_LPT_SSP },
  1223. { "INT3431", LPSS_LPT_SSP },
  1224. { "80860F0E", LPSS_BYT_SSP },
  1225. { "8086228E", LPSS_BSW_SSP },
  1226. { },
  1227. };
  1228. MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
  1229. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1230. {
  1231. unsigned int devid;
  1232. int port_id = -1;
  1233. if (adev && adev->pnp.unique_id &&
  1234. !kstrtouint(adev->pnp.unique_id, 0, &devid))
  1235. port_id = devid;
  1236. return port_id;
  1237. }
  1238. #else /* !CONFIG_ACPI */
  1239. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1240. {
  1241. return -1;
  1242. }
  1243. #endif
  1244. /*
  1245. * PCI IDs of compound devices that integrate both host controller and private
  1246. * integrated DMA engine. Please note these are not used in module
  1247. * autoloading and probing in this module but matching the LPSS SSP type.
  1248. */
  1249. static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
  1250. /* SPT-LP */
  1251. { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
  1252. { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
  1253. /* SPT-H */
  1254. { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
  1255. { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
  1256. /* KBL-H */
  1257. { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
  1258. { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
  1259. /* BXT A-Step */
  1260. { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
  1261. { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
  1262. { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
  1263. /* BXT B-Step */
  1264. { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
  1265. { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
  1266. { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
  1267. /* GLK */
  1268. { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
  1269. { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
  1270. { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
  1271. /* APL */
  1272. { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
  1273. { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
  1274. { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
  1275. /* CNL-LP */
  1276. { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
  1277. { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
  1278. { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
  1279. /* CNL-H */
  1280. { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
  1281. { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
  1282. { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
  1283. { },
  1284. };
  1285. static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
  1286. {
  1287. struct device *dev = param;
  1288. if (dev != chan->device->dev->parent)
  1289. return false;
  1290. return true;
  1291. }
  1292. static struct pxa2xx_spi_master *
  1293. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1294. {
  1295. struct pxa2xx_spi_master *pdata;
  1296. struct acpi_device *adev;
  1297. struct ssp_device *ssp;
  1298. struct resource *res;
  1299. const struct acpi_device_id *adev_id = NULL;
  1300. const struct pci_device_id *pcidev_id = NULL;
  1301. int type;
  1302. adev = ACPI_COMPANION(&pdev->dev);
  1303. if (dev_is_pci(pdev->dev.parent))
  1304. pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
  1305. to_pci_dev(pdev->dev.parent));
  1306. else if (adev)
  1307. adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  1308. &pdev->dev);
  1309. else
  1310. return NULL;
  1311. if (adev_id)
  1312. type = (int)adev_id->driver_data;
  1313. else if (pcidev_id)
  1314. type = (int)pcidev_id->driver_data;
  1315. else
  1316. return NULL;
  1317. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1318. if (!pdata)
  1319. return NULL;
  1320. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1321. if (!res)
  1322. return NULL;
  1323. ssp = &pdata->ssp;
  1324. ssp->phys_base = res->start;
  1325. ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  1326. if (IS_ERR(ssp->mmio_base))
  1327. return NULL;
  1328. if (pcidev_id) {
  1329. pdata->tx_param = pdev->dev.parent;
  1330. pdata->rx_param = pdev->dev.parent;
  1331. pdata->dma_filter = pxa2xx_spi_idma_filter;
  1332. }
  1333. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  1334. ssp->irq = platform_get_irq(pdev, 0);
  1335. ssp->type = type;
  1336. ssp->pdev = pdev;
  1337. ssp->port_id = pxa2xx_spi_get_port_id(adev);
  1338. pdata->num_chipselect = 1;
  1339. pdata->enable_dma = true;
  1340. return pdata;
  1341. }
  1342. #else /* !CONFIG_PCI */
  1343. static inline struct pxa2xx_spi_master *
  1344. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1345. {
  1346. return NULL;
  1347. }
  1348. #endif
  1349. static int pxa2xx_spi_fw_translate_cs(struct spi_master *master, unsigned cs)
  1350. {
  1351. struct driver_data *drv_data = spi_master_get_devdata(master);
  1352. if (has_acpi_companion(&drv_data->pdev->dev)) {
  1353. switch (drv_data->ssp_type) {
  1354. /*
  1355. * For Atoms the ACPI DeviceSelection used by the Windows
  1356. * driver starts from 1 instead of 0 so translate it here
  1357. * to match what Linux expects.
  1358. */
  1359. case LPSS_BYT_SSP:
  1360. case LPSS_BSW_SSP:
  1361. return cs - 1;
  1362. default:
  1363. break;
  1364. }
  1365. }
  1366. return cs;
  1367. }
  1368. static int pxa2xx_spi_probe(struct platform_device *pdev)
  1369. {
  1370. struct device *dev = &pdev->dev;
  1371. struct pxa2xx_spi_master *platform_info;
  1372. struct spi_master *master;
  1373. struct driver_data *drv_data;
  1374. struct ssp_device *ssp;
  1375. const struct lpss_config *config;
  1376. int status, count;
  1377. u32 tmp;
  1378. platform_info = dev_get_platdata(dev);
  1379. if (!platform_info) {
  1380. platform_info = pxa2xx_spi_init_pdata(pdev);
  1381. if (!platform_info) {
  1382. dev_err(&pdev->dev, "missing platform data\n");
  1383. return -ENODEV;
  1384. }
  1385. }
  1386. ssp = pxa_ssp_request(pdev->id, pdev->name);
  1387. if (!ssp)
  1388. ssp = &platform_info->ssp;
  1389. if (!ssp->mmio_base) {
  1390. dev_err(&pdev->dev, "failed to get ssp\n");
  1391. return -ENODEV;
  1392. }
  1393. master = spi_alloc_master(dev, sizeof(struct driver_data));
  1394. if (!master) {
  1395. dev_err(&pdev->dev, "cannot alloc spi_master\n");
  1396. pxa_ssp_free(ssp);
  1397. return -ENOMEM;
  1398. }
  1399. drv_data = spi_master_get_devdata(master);
  1400. drv_data->master = master;
  1401. drv_data->master_info = platform_info;
  1402. drv_data->pdev = pdev;
  1403. drv_data->ssp = ssp;
  1404. master->dev.of_node = pdev->dev.of_node;
  1405. /* the spi->mode bits understood by this driver: */
  1406. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1407. master->bus_num = ssp->port_id;
  1408. master->dma_alignment = DMA_ALIGNMENT;
  1409. master->cleanup = cleanup;
  1410. master->setup = setup;
  1411. master->transfer_one_message = pxa2xx_spi_transfer_one_message;
  1412. master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
  1413. master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
  1414. master->auto_runtime_pm = true;
  1415. master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
  1416. drv_data->ssp_type = ssp->type;
  1417. drv_data->ioaddr = ssp->mmio_base;
  1418. drv_data->ssdr_physical = ssp->phys_base + SSDR;
  1419. if (pxa25x_ssp_comp(drv_data)) {
  1420. switch (drv_data->ssp_type) {
  1421. case QUARK_X1000_SSP:
  1422. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1423. break;
  1424. default:
  1425. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1426. break;
  1427. }
  1428. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
  1429. drv_data->dma_cr1 = 0;
  1430. drv_data->clear_sr = SSSR_ROR;
  1431. drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1432. } else {
  1433. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1434. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  1435. drv_data->dma_cr1 = DEFAULT_DMA_CR1;
  1436. drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
  1437. drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1438. }
  1439. status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
  1440. drv_data);
  1441. if (status < 0) {
  1442. dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
  1443. goto out_error_master_alloc;
  1444. }
  1445. /* Setup DMA if requested */
  1446. if (platform_info->enable_dma) {
  1447. status = pxa2xx_spi_dma_setup(drv_data);
  1448. if (status) {
  1449. dev_dbg(dev, "no DMA channels available, using PIO\n");
  1450. platform_info->enable_dma = false;
  1451. } else {
  1452. master->can_dma = pxa2xx_spi_can_dma;
  1453. }
  1454. }
  1455. /* Enable SOC clock */
  1456. clk_prepare_enable(ssp->clk);
  1457. master->max_speed_hz = clk_get_rate(ssp->clk);
  1458. /* Load default SSP configuration */
  1459. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1460. switch (drv_data->ssp_type) {
  1461. case QUARK_X1000_SSP:
  1462. tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
  1463. QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
  1464. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1465. /* using the Motorola SPI protocol and use 8 bit frame */
  1466. tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
  1467. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1468. break;
  1469. case CE4100_SSP:
  1470. tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
  1471. CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
  1472. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1473. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1474. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1475. break;
  1476. default:
  1477. tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
  1478. SSCR1_TxTresh(TX_THRESH_DFLT);
  1479. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1480. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1481. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1482. break;
  1483. }
  1484. if (!pxa25x_ssp_comp(drv_data))
  1485. pxa2xx_spi_write(drv_data, SSTO, 0);
  1486. if (!is_quark_x1000_ssp(drv_data))
  1487. pxa2xx_spi_write(drv_data, SSPSP, 0);
  1488. if (is_lpss_ssp(drv_data)) {
  1489. lpss_ssp_setup(drv_data);
  1490. config = lpss_get_config(drv_data);
  1491. if (config->reg_capabilities >= 0) {
  1492. tmp = __lpss_ssp_read_priv(drv_data,
  1493. config->reg_capabilities);
  1494. tmp &= LPSS_CAPS_CS_EN_MASK;
  1495. tmp >>= LPSS_CAPS_CS_EN_SHIFT;
  1496. platform_info->num_chipselect = ffz(tmp);
  1497. } else if (config->cs_num) {
  1498. platform_info->num_chipselect = config->cs_num;
  1499. }
  1500. }
  1501. master->num_chipselect = platform_info->num_chipselect;
  1502. count = gpiod_count(&pdev->dev, "cs");
  1503. if (count > 0) {
  1504. int i;
  1505. master->num_chipselect = max_t(int, count,
  1506. master->num_chipselect);
  1507. drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
  1508. master->num_chipselect, sizeof(struct gpio_desc *),
  1509. GFP_KERNEL);
  1510. if (!drv_data->cs_gpiods) {
  1511. status = -ENOMEM;
  1512. goto out_error_clock_enabled;
  1513. }
  1514. for (i = 0; i < master->num_chipselect; i++) {
  1515. struct gpio_desc *gpiod;
  1516. gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
  1517. if (IS_ERR(gpiod)) {
  1518. /* Means use native chip select */
  1519. if (PTR_ERR(gpiod) == -ENOENT)
  1520. continue;
  1521. status = (int)PTR_ERR(gpiod);
  1522. goto out_error_clock_enabled;
  1523. } else {
  1524. drv_data->cs_gpiods[i] = gpiod;
  1525. }
  1526. }
  1527. }
  1528. tasklet_init(&drv_data->pump_transfers, pump_transfers,
  1529. (unsigned long)drv_data);
  1530. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1531. pm_runtime_use_autosuspend(&pdev->dev);
  1532. pm_runtime_set_active(&pdev->dev);
  1533. pm_runtime_enable(&pdev->dev);
  1534. /* Register with the SPI framework */
  1535. platform_set_drvdata(pdev, drv_data);
  1536. status = devm_spi_register_master(&pdev->dev, master);
  1537. if (status != 0) {
  1538. dev_err(&pdev->dev, "problem registering spi master\n");
  1539. goto out_error_clock_enabled;
  1540. }
  1541. return status;
  1542. out_error_clock_enabled:
  1543. clk_disable_unprepare(ssp->clk);
  1544. pxa2xx_spi_dma_release(drv_data);
  1545. free_irq(ssp->irq, drv_data);
  1546. out_error_master_alloc:
  1547. spi_master_put(master);
  1548. pxa_ssp_free(ssp);
  1549. return status;
  1550. }
  1551. static int pxa2xx_spi_remove(struct platform_device *pdev)
  1552. {
  1553. struct driver_data *drv_data = platform_get_drvdata(pdev);
  1554. struct ssp_device *ssp;
  1555. if (!drv_data)
  1556. return 0;
  1557. ssp = drv_data->ssp;
  1558. pm_runtime_get_sync(&pdev->dev);
  1559. /* Disable the SSP at the peripheral and SOC level */
  1560. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1561. clk_disable_unprepare(ssp->clk);
  1562. /* Release DMA */
  1563. if (drv_data->master_info->enable_dma)
  1564. pxa2xx_spi_dma_release(drv_data);
  1565. pm_runtime_put_noidle(&pdev->dev);
  1566. pm_runtime_disable(&pdev->dev);
  1567. /* Release IRQ */
  1568. free_irq(ssp->irq, drv_data);
  1569. /* Release SSP */
  1570. pxa_ssp_free(ssp);
  1571. return 0;
  1572. }
  1573. static void pxa2xx_spi_shutdown(struct platform_device *pdev)
  1574. {
  1575. int status = 0;
  1576. if ((status = pxa2xx_spi_remove(pdev)) != 0)
  1577. dev_err(&pdev->dev, "shutdown failed with %d\n", status);
  1578. }
  1579. #ifdef CONFIG_PM_SLEEP
  1580. static int pxa2xx_spi_suspend(struct device *dev)
  1581. {
  1582. struct driver_data *drv_data = dev_get_drvdata(dev);
  1583. struct ssp_device *ssp = drv_data->ssp;
  1584. int status;
  1585. status = spi_master_suspend(drv_data->master);
  1586. if (status != 0)
  1587. return status;
  1588. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1589. if (!pm_runtime_suspended(dev))
  1590. clk_disable_unprepare(ssp->clk);
  1591. return 0;
  1592. }
  1593. static int pxa2xx_spi_resume(struct device *dev)
  1594. {
  1595. struct driver_data *drv_data = dev_get_drvdata(dev);
  1596. struct ssp_device *ssp = drv_data->ssp;
  1597. int status;
  1598. /* Enable the SSP clock */
  1599. if (!pm_runtime_suspended(dev))
  1600. clk_prepare_enable(ssp->clk);
  1601. /* Restore LPSS private register bits */
  1602. if (is_lpss_ssp(drv_data))
  1603. lpss_ssp_setup(drv_data);
  1604. /* Start the queue running */
  1605. status = spi_master_resume(drv_data->master);
  1606. if (status != 0) {
  1607. dev_err(dev, "problem starting queue (%d)\n", status);
  1608. return status;
  1609. }
  1610. return 0;
  1611. }
  1612. #endif
  1613. #ifdef CONFIG_PM
  1614. static int pxa2xx_spi_runtime_suspend(struct device *dev)
  1615. {
  1616. struct driver_data *drv_data = dev_get_drvdata(dev);
  1617. clk_disable_unprepare(drv_data->ssp->clk);
  1618. return 0;
  1619. }
  1620. static int pxa2xx_spi_runtime_resume(struct device *dev)
  1621. {
  1622. struct driver_data *drv_data = dev_get_drvdata(dev);
  1623. clk_prepare_enable(drv_data->ssp->clk);
  1624. return 0;
  1625. }
  1626. #endif
  1627. static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
  1628. SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
  1629. SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
  1630. pxa2xx_spi_runtime_resume, NULL)
  1631. };
  1632. static struct platform_driver driver = {
  1633. .driver = {
  1634. .name = "pxa2xx-spi",
  1635. .pm = &pxa2xx_spi_pm_ops,
  1636. .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
  1637. },
  1638. .probe = pxa2xx_spi_probe,
  1639. .remove = pxa2xx_spi_remove,
  1640. .shutdown = pxa2xx_spi_shutdown,
  1641. };
  1642. static int __init pxa2xx_spi_init(void)
  1643. {
  1644. return platform_driver_register(&driver);
  1645. }
  1646. subsys_initcall(pxa2xx_spi_init);
  1647. static void __exit pxa2xx_spi_exit(void)
  1648. {
  1649. platform_driver_unregister(&driver);
  1650. }
  1651. module_exit(pxa2xx_spi_exit);