pci.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  2. * Sonics Silicon Backplane PCI-Hostbus related functions.
  3. *
  4. * Copyright (C) 2005-2006 Michael Buesch <mb@bu3sch.de>
  5. * Copyright (C) 2005 Martin Langer <martin-langer@gmx.de>
  6. * Copyright (C) 2005 Stefano Brivio <st3@riseup.net>
  7. * Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org>
  8. * Copyright (C) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  9. *
  10. * Derived from the Broadcom 4400 device driver.
  11. * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  12. * Fixed by Pekka Pietikainen (pp@ee.oulu.fi)
  13. * Copyright (C) 2006 Broadcom Corporation.
  14. *
  15. * Licensed under the GNU/GPL. See COPYING for details.
  16. */
  17. #include <linux/ssb/ssb.h>
  18. #include <linux/ssb/ssb_regs.h>
  19. #include <linux/slab.h>
  20. #include <linux/pci.h>
  21. #include <linux/delay.h>
  22. #include "ssb_private.h"
  23. /* Define the following to 1 to enable a printk on each coreswitch. */
  24. #define SSB_VERBOSE_PCICORESWITCH_DEBUG 0
  25. /* Lowlevel coreswitching */
  26. int ssb_pci_switch_coreidx(struct ssb_bus *bus, u8 coreidx)
  27. {
  28. int err;
  29. int attempts = 0;
  30. u32 cur_core;
  31. while (1) {
  32. err = pci_write_config_dword(bus->host_pci, SSB_BAR0_WIN,
  33. (coreidx * SSB_CORE_SIZE)
  34. + SSB_ENUM_BASE);
  35. if (err)
  36. goto error;
  37. err = pci_read_config_dword(bus->host_pci, SSB_BAR0_WIN,
  38. &cur_core);
  39. if (err)
  40. goto error;
  41. cur_core = (cur_core - SSB_ENUM_BASE)
  42. / SSB_CORE_SIZE;
  43. if (cur_core == coreidx)
  44. break;
  45. if (attempts++ > SSB_BAR0_MAX_RETRIES)
  46. goto error;
  47. udelay(10);
  48. }
  49. return 0;
  50. error:
  51. ssb_printk(KERN_ERR PFX "Failed to switch to core %u\n", coreidx);
  52. return -ENODEV;
  53. }
  54. int ssb_pci_switch_core(struct ssb_bus *bus,
  55. struct ssb_device *dev)
  56. {
  57. int err;
  58. unsigned long flags;
  59. #if SSB_VERBOSE_PCICORESWITCH_DEBUG
  60. ssb_printk(KERN_INFO PFX
  61. "Switching to %s core, index %d\n",
  62. ssb_core_name(dev->id.coreid),
  63. dev->core_index);
  64. #endif
  65. spin_lock_irqsave(&bus->bar_lock, flags);
  66. err = ssb_pci_switch_coreidx(bus, dev->core_index);
  67. if (!err)
  68. bus->mapped_device = dev;
  69. spin_unlock_irqrestore(&bus->bar_lock, flags);
  70. return err;
  71. }
  72. /* Enable/disable the on board crystal oscillator and/or PLL. */
  73. int ssb_pci_xtal(struct ssb_bus *bus, u32 what, int turn_on)
  74. {
  75. int err;
  76. u32 in, out, outenable;
  77. u16 pci_status;
  78. if (bus->bustype != SSB_BUSTYPE_PCI)
  79. return 0;
  80. err = pci_read_config_dword(bus->host_pci, SSB_GPIO_IN, &in);
  81. if (err)
  82. goto err_pci;
  83. err = pci_read_config_dword(bus->host_pci, SSB_GPIO_OUT, &out);
  84. if (err)
  85. goto err_pci;
  86. err = pci_read_config_dword(bus->host_pci, SSB_GPIO_OUT_ENABLE, &outenable);
  87. if (err)
  88. goto err_pci;
  89. outenable |= what;
  90. if (turn_on) {
  91. /* Avoid glitching the clock if GPRS is already using it.
  92. * We can't actually read the state of the PLLPD so we infer it
  93. * by the value of XTAL_PU which *is* readable via gpioin.
  94. */
  95. if (!(in & SSB_GPIO_XTAL)) {
  96. if (what & SSB_GPIO_XTAL) {
  97. /* Turn the crystal on */
  98. out |= SSB_GPIO_XTAL;
  99. if (what & SSB_GPIO_PLL)
  100. out |= SSB_GPIO_PLL;
  101. err = pci_write_config_dword(bus->host_pci, SSB_GPIO_OUT, out);
  102. if (err)
  103. goto err_pci;
  104. err = pci_write_config_dword(bus->host_pci, SSB_GPIO_OUT_ENABLE,
  105. outenable);
  106. if (err)
  107. goto err_pci;
  108. msleep(1);
  109. }
  110. if (what & SSB_GPIO_PLL) {
  111. /* Turn the PLL on */
  112. out &= ~SSB_GPIO_PLL;
  113. err = pci_write_config_dword(bus->host_pci, SSB_GPIO_OUT, out);
  114. if (err)
  115. goto err_pci;
  116. msleep(5);
  117. }
  118. }
  119. err = pci_read_config_word(bus->host_pci, PCI_STATUS, &pci_status);
  120. if (err)
  121. goto err_pci;
  122. pci_status &= ~PCI_STATUS_SIG_TARGET_ABORT;
  123. err = pci_write_config_word(bus->host_pci, PCI_STATUS, pci_status);
  124. if (err)
  125. goto err_pci;
  126. } else {
  127. if (what & SSB_GPIO_XTAL) {
  128. /* Turn the crystal off */
  129. out &= ~SSB_GPIO_XTAL;
  130. }
  131. if (what & SSB_GPIO_PLL) {
  132. /* Turn the PLL off */
  133. out |= SSB_GPIO_PLL;
  134. }
  135. err = pci_write_config_dword(bus->host_pci, SSB_GPIO_OUT, out);
  136. if (err)
  137. goto err_pci;
  138. err = pci_write_config_dword(bus->host_pci, SSB_GPIO_OUT_ENABLE, outenable);
  139. if (err)
  140. goto err_pci;
  141. }
  142. out:
  143. return err;
  144. err_pci:
  145. printk(KERN_ERR PFX "Error: ssb_pci_xtal() could not access PCI config space!\n");
  146. err = -EBUSY;
  147. goto out;
  148. }
  149. /* Get the word-offset for a SSB_SPROM_XXX define. */
  150. #define SPOFF(offset) ((offset) / sizeof(u16))
  151. /* Helper to extract some _offset, which is one of the SSB_SPROM_XXX defines. */
  152. #define SPEX16(_outvar, _offset, _mask, _shift) \
  153. out->_outvar = ((in[SPOFF(_offset)] & (_mask)) >> (_shift))
  154. #define SPEX32(_outvar, _offset, _mask, _shift) \
  155. out->_outvar = ((((u32)in[SPOFF((_offset)+2)] << 16 | \
  156. in[SPOFF(_offset)]) & (_mask)) >> (_shift))
  157. #define SPEX(_outvar, _offset, _mask, _shift) \
  158. SPEX16(_outvar, _offset, _mask, _shift)
  159. static inline u8 ssb_crc8(u8 crc, u8 data)
  160. {
  161. /* Polynomial: x^8 + x^7 + x^6 + x^4 + x^2 + 1 */
  162. static const u8 t[] = {
  163. 0x00, 0xF7, 0xB9, 0x4E, 0x25, 0xD2, 0x9C, 0x6B,
  164. 0x4A, 0xBD, 0xF3, 0x04, 0x6F, 0x98, 0xD6, 0x21,
  165. 0x94, 0x63, 0x2D, 0xDA, 0xB1, 0x46, 0x08, 0xFF,
  166. 0xDE, 0x29, 0x67, 0x90, 0xFB, 0x0C, 0x42, 0xB5,
  167. 0x7F, 0x88, 0xC6, 0x31, 0x5A, 0xAD, 0xE3, 0x14,
  168. 0x35, 0xC2, 0x8C, 0x7B, 0x10, 0xE7, 0xA9, 0x5E,
  169. 0xEB, 0x1C, 0x52, 0xA5, 0xCE, 0x39, 0x77, 0x80,
  170. 0xA1, 0x56, 0x18, 0xEF, 0x84, 0x73, 0x3D, 0xCA,
  171. 0xFE, 0x09, 0x47, 0xB0, 0xDB, 0x2C, 0x62, 0x95,
  172. 0xB4, 0x43, 0x0D, 0xFA, 0x91, 0x66, 0x28, 0xDF,
  173. 0x6A, 0x9D, 0xD3, 0x24, 0x4F, 0xB8, 0xF6, 0x01,
  174. 0x20, 0xD7, 0x99, 0x6E, 0x05, 0xF2, 0xBC, 0x4B,
  175. 0x81, 0x76, 0x38, 0xCF, 0xA4, 0x53, 0x1D, 0xEA,
  176. 0xCB, 0x3C, 0x72, 0x85, 0xEE, 0x19, 0x57, 0xA0,
  177. 0x15, 0xE2, 0xAC, 0x5B, 0x30, 0xC7, 0x89, 0x7E,
  178. 0x5F, 0xA8, 0xE6, 0x11, 0x7A, 0x8D, 0xC3, 0x34,
  179. 0xAB, 0x5C, 0x12, 0xE5, 0x8E, 0x79, 0x37, 0xC0,
  180. 0xE1, 0x16, 0x58, 0xAF, 0xC4, 0x33, 0x7D, 0x8A,
  181. 0x3F, 0xC8, 0x86, 0x71, 0x1A, 0xED, 0xA3, 0x54,
  182. 0x75, 0x82, 0xCC, 0x3B, 0x50, 0xA7, 0xE9, 0x1E,
  183. 0xD4, 0x23, 0x6D, 0x9A, 0xF1, 0x06, 0x48, 0xBF,
  184. 0x9E, 0x69, 0x27, 0xD0, 0xBB, 0x4C, 0x02, 0xF5,
  185. 0x40, 0xB7, 0xF9, 0x0E, 0x65, 0x92, 0xDC, 0x2B,
  186. 0x0A, 0xFD, 0xB3, 0x44, 0x2F, 0xD8, 0x96, 0x61,
  187. 0x55, 0xA2, 0xEC, 0x1B, 0x70, 0x87, 0xC9, 0x3E,
  188. 0x1F, 0xE8, 0xA6, 0x51, 0x3A, 0xCD, 0x83, 0x74,
  189. 0xC1, 0x36, 0x78, 0x8F, 0xE4, 0x13, 0x5D, 0xAA,
  190. 0x8B, 0x7C, 0x32, 0xC5, 0xAE, 0x59, 0x17, 0xE0,
  191. 0x2A, 0xDD, 0x93, 0x64, 0x0F, 0xF8, 0xB6, 0x41,
  192. 0x60, 0x97, 0xD9, 0x2E, 0x45, 0xB2, 0xFC, 0x0B,
  193. 0xBE, 0x49, 0x07, 0xF0, 0x9B, 0x6C, 0x22, 0xD5,
  194. 0xF4, 0x03, 0x4D, 0xBA, 0xD1, 0x26, 0x68, 0x9F,
  195. };
  196. return t[crc ^ data];
  197. }
  198. static u8 ssb_sprom_crc(const u16 *sprom, u16 size)
  199. {
  200. int word;
  201. u8 crc = 0xFF;
  202. for (word = 0; word < size - 1; word++) {
  203. crc = ssb_crc8(crc, sprom[word] & 0x00FF);
  204. crc = ssb_crc8(crc, (sprom[word] & 0xFF00) >> 8);
  205. }
  206. crc = ssb_crc8(crc, sprom[size - 1] & 0x00FF);
  207. crc ^= 0xFF;
  208. return crc;
  209. }
  210. static int sprom_check_crc(const u16 *sprom, size_t size)
  211. {
  212. u8 crc;
  213. u8 expected_crc;
  214. u16 tmp;
  215. crc = ssb_sprom_crc(sprom, size);
  216. tmp = sprom[size - 1] & SSB_SPROM_REVISION_CRC;
  217. expected_crc = tmp >> SSB_SPROM_REVISION_CRC_SHIFT;
  218. if (crc != expected_crc)
  219. return -EPROTO;
  220. return 0;
  221. }
  222. static int sprom_do_read(struct ssb_bus *bus, u16 *sprom)
  223. {
  224. int i;
  225. for (i = 0; i < bus->sprom_size; i++)
  226. sprom[i] = ioread16(bus->mmio + bus->sprom_offset + (i * 2));
  227. return 0;
  228. }
  229. static int sprom_do_write(struct ssb_bus *bus, const u16 *sprom)
  230. {
  231. struct pci_dev *pdev = bus->host_pci;
  232. int i, err;
  233. u32 spromctl;
  234. u16 size = bus->sprom_size;
  235. ssb_printk(KERN_NOTICE PFX "Writing SPROM. Do NOT turn off the power! Please stand by...\n");
  236. err = pci_read_config_dword(pdev, SSB_SPROMCTL, &spromctl);
  237. if (err)
  238. goto err_ctlreg;
  239. spromctl |= SSB_SPROMCTL_WE;
  240. err = pci_write_config_dword(pdev, SSB_SPROMCTL, spromctl);
  241. if (err)
  242. goto err_ctlreg;
  243. ssb_printk(KERN_NOTICE PFX "[ 0%%");
  244. msleep(500);
  245. for (i = 0; i < size; i++) {
  246. if (i == size / 4)
  247. ssb_printk("25%%");
  248. else if (i == size / 2)
  249. ssb_printk("50%%");
  250. else if (i == (size * 3) / 4)
  251. ssb_printk("75%%");
  252. else if (i % 2)
  253. ssb_printk(".");
  254. writew(sprom[i], bus->mmio + bus->sprom_offset + (i * 2));
  255. mmiowb();
  256. msleep(20);
  257. }
  258. err = pci_read_config_dword(pdev, SSB_SPROMCTL, &spromctl);
  259. if (err)
  260. goto err_ctlreg;
  261. spromctl &= ~SSB_SPROMCTL_WE;
  262. err = pci_write_config_dword(pdev, SSB_SPROMCTL, spromctl);
  263. if (err)
  264. goto err_ctlreg;
  265. msleep(500);
  266. ssb_printk("100%% ]\n");
  267. ssb_printk(KERN_NOTICE PFX "SPROM written.\n");
  268. return 0;
  269. err_ctlreg:
  270. ssb_printk(KERN_ERR PFX "Could not access SPROM control register.\n");
  271. return err;
  272. }
  273. static s8 r123_extract_antgain(u8 sprom_revision, const u16 *in,
  274. u16 mask, u16 shift)
  275. {
  276. u16 v;
  277. u8 gain;
  278. v = in[SPOFF(SSB_SPROM1_AGAIN)];
  279. gain = (v & mask) >> shift;
  280. if (gain == 0xFF)
  281. gain = 2; /* If unset use 2dBm */
  282. if (sprom_revision == 1) {
  283. /* Convert to Q5.2 */
  284. gain <<= 2;
  285. } else {
  286. /* Q5.2 Fractional part is stored in 0xC0 */
  287. gain = ((gain & 0xC0) >> 6) | ((gain & 0x3F) << 2);
  288. }
  289. return (s8)gain;
  290. }
  291. static void sprom_extract_r123(struct ssb_sprom *out, const u16 *in)
  292. {
  293. int i;
  294. u16 v;
  295. s8 gain;
  296. u16 loc[3];
  297. if (out->revision == 3) /* rev 3 moved MAC */
  298. loc[0] = SSB_SPROM3_IL0MAC;
  299. else {
  300. loc[0] = SSB_SPROM1_IL0MAC;
  301. loc[1] = SSB_SPROM1_ET0MAC;
  302. loc[2] = SSB_SPROM1_ET1MAC;
  303. }
  304. for (i = 0; i < 3; i++) {
  305. v = in[SPOFF(loc[0]) + i];
  306. *(((__be16 *)out->il0mac) + i) = cpu_to_be16(v);
  307. }
  308. if (out->revision < 3) { /* only rev 1-2 have et0, et1 */
  309. for (i = 0; i < 3; i++) {
  310. v = in[SPOFF(loc[1]) + i];
  311. *(((__be16 *)out->et0mac) + i) = cpu_to_be16(v);
  312. }
  313. for (i = 0; i < 3; i++) {
  314. v = in[SPOFF(loc[2]) + i];
  315. *(((__be16 *)out->et1mac) + i) = cpu_to_be16(v);
  316. }
  317. }
  318. SPEX(et0phyaddr, SSB_SPROM1_ETHPHY, SSB_SPROM1_ETHPHY_ET0A, 0);
  319. SPEX(et1phyaddr, SSB_SPROM1_ETHPHY, SSB_SPROM1_ETHPHY_ET1A,
  320. SSB_SPROM1_ETHPHY_ET1A_SHIFT);
  321. SPEX(et0mdcport, SSB_SPROM1_ETHPHY, SSB_SPROM1_ETHPHY_ET0M, 14);
  322. SPEX(et1mdcport, SSB_SPROM1_ETHPHY, SSB_SPROM1_ETHPHY_ET1M, 15);
  323. SPEX(board_rev, SSB_SPROM1_BINF, SSB_SPROM1_BINF_BREV, 0);
  324. SPEX(country_code, SSB_SPROM1_BINF, SSB_SPROM1_BINF_CCODE,
  325. SSB_SPROM1_BINF_CCODE_SHIFT);
  326. SPEX(ant_available_a, SSB_SPROM1_BINF, SSB_SPROM1_BINF_ANTA,
  327. SSB_SPROM1_BINF_ANTA_SHIFT);
  328. SPEX(ant_available_bg, SSB_SPROM1_BINF, SSB_SPROM1_BINF_ANTBG,
  329. SSB_SPROM1_BINF_ANTBG_SHIFT);
  330. SPEX(pa0b0, SSB_SPROM1_PA0B0, 0xFFFF, 0);
  331. SPEX(pa0b1, SSB_SPROM1_PA0B1, 0xFFFF, 0);
  332. SPEX(pa0b2, SSB_SPROM1_PA0B2, 0xFFFF, 0);
  333. SPEX(pa1b0, SSB_SPROM1_PA1B0, 0xFFFF, 0);
  334. SPEX(pa1b1, SSB_SPROM1_PA1B1, 0xFFFF, 0);
  335. SPEX(pa1b2, SSB_SPROM1_PA1B2, 0xFFFF, 0);
  336. SPEX(gpio0, SSB_SPROM1_GPIOA, SSB_SPROM1_GPIOA_P0, 0);
  337. SPEX(gpio1, SSB_SPROM1_GPIOA, SSB_SPROM1_GPIOA_P1,
  338. SSB_SPROM1_GPIOA_P1_SHIFT);
  339. SPEX(gpio2, SSB_SPROM1_GPIOB, SSB_SPROM1_GPIOB_P2, 0);
  340. SPEX(gpio3, SSB_SPROM1_GPIOB, SSB_SPROM1_GPIOB_P3,
  341. SSB_SPROM1_GPIOB_P3_SHIFT);
  342. SPEX(maxpwr_a, SSB_SPROM1_MAXPWR, SSB_SPROM1_MAXPWR_A,
  343. SSB_SPROM1_MAXPWR_A_SHIFT);
  344. SPEX(maxpwr_bg, SSB_SPROM1_MAXPWR, SSB_SPROM1_MAXPWR_BG, 0);
  345. SPEX(itssi_a, SSB_SPROM1_ITSSI, SSB_SPROM1_ITSSI_A,
  346. SSB_SPROM1_ITSSI_A_SHIFT);
  347. SPEX(itssi_bg, SSB_SPROM1_ITSSI, SSB_SPROM1_ITSSI_BG, 0);
  348. SPEX(boardflags_lo, SSB_SPROM1_BFLLO, 0xFFFF, 0);
  349. if (out->revision >= 2)
  350. SPEX(boardflags_hi, SSB_SPROM2_BFLHI, 0xFFFF, 0);
  351. /* Extract the antenna gain values. */
  352. gain = r123_extract_antgain(out->revision, in,
  353. SSB_SPROM1_AGAIN_BG,
  354. SSB_SPROM1_AGAIN_BG_SHIFT);
  355. out->antenna_gain.ghz24.a0 = gain;
  356. out->antenna_gain.ghz24.a1 = gain;
  357. out->antenna_gain.ghz24.a2 = gain;
  358. out->antenna_gain.ghz24.a3 = gain;
  359. gain = r123_extract_antgain(out->revision, in,
  360. SSB_SPROM1_AGAIN_A,
  361. SSB_SPROM1_AGAIN_A_SHIFT);
  362. out->antenna_gain.ghz5.a0 = gain;
  363. out->antenna_gain.ghz5.a1 = gain;
  364. out->antenna_gain.ghz5.a2 = gain;
  365. out->antenna_gain.ghz5.a3 = gain;
  366. }
  367. /* Revs 4 5 and 8 have partially shared layout */
  368. static void sprom_extract_r458(struct ssb_sprom *out, const u16 *in)
  369. {
  370. SPEX(txpid2g[0], SSB_SPROM4_TXPID2G01,
  371. SSB_SPROM4_TXPID2G0, SSB_SPROM4_TXPID2G0_SHIFT);
  372. SPEX(txpid2g[1], SSB_SPROM4_TXPID2G01,
  373. SSB_SPROM4_TXPID2G1, SSB_SPROM4_TXPID2G1_SHIFT);
  374. SPEX(txpid2g[2], SSB_SPROM4_TXPID2G23,
  375. SSB_SPROM4_TXPID2G2, SSB_SPROM4_TXPID2G2_SHIFT);
  376. SPEX(txpid2g[3], SSB_SPROM4_TXPID2G23,
  377. SSB_SPROM4_TXPID2G3, SSB_SPROM4_TXPID2G3_SHIFT);
  378. SPEX(txpid5gl[0], SSB_SPROM4_TXPID5GL01,
  379. SSB_SPROM4_TXPID5GL0, SSB_SPROM4_TXPID5GL0_SHIFT);
  380. SPEX(txpid5gl[1], SSB_SPROM4_TXPID5GL01,
  381. SSB_SPROM4_TXPID5GL1, SSB_SPROM4_TXPID5GL1_SHIFT);
  382. SPEX(txpid5gl[2], SSB_SPROM4_TXPID5GL23,
  383. SSB_SPROM4_TXPID5GL2, SSB_SPROM4_TXPID5GL2_SHIFT);
  384. SPEX(txpid5gl[3], SSB_SPROM4_TXPID5GL23,
  385. SSB_SPROM4_TXPID5GL3, SSB_SPROM4_TXPID5GL3_SHIFT);
  386. SPEX(txpid5g[0], SSB_SPROM4_TXPID5G01,
  387. SSB_SPROM4_TXPID5G0, SSB_SPROM4_TXPID5G0_SHIFT);
  388. SPEX(txpid5g[1], SSB_SPROM4_TXPID5G01,
  389. SSB_SPROM4_TXPID5G1, SSB_SPROM4_TXPID5G1_SHIFT);
  390. SPEX(txpid5g[2], SSB_SPROM4_TXPID5G23,
  391. SSB_SPROM4_TXPID5G2, SSB_SPROM4_TXPID5G2_SHIFT);
  392. SPEX(txpid5g[3], SSB_SPROM4_TXPID5G23,
  393. SSB_SPROM4_TXPID5G3, SSB_SPROM4_TXPID5G3_SHIFT);
  394. SPEX(txpid5gh[0], SSB_SPROM4_TXPID5GH01,
  395. SSB_SPROM4_TXPID5GH0, SSB_SPROM4_TXPID5GH0_SHIFT);
  396. SPEX(txpid5gh[1], SSB_SPROM4_TXPID5GH01,
  397. SSB_SPROM4_TXPID5GH1, SSB_SPROM4_TXPID5GH1_SHIFT);
  398. SPEX(txpid5gh[2], SSB_SPROM4_TXPID5GH23,
  399. SSB_SPROM4_TXPID5GH2, SSB_SPROM4_TXPID5GH2_SHIFT);
  400. SPEX(txpid5gh[3], SSB_SPROM4_TXPID5GH23,
  401. SSB_SPROM4_TXPID5GH3, SSB_SPROM4_TXPID5GH3_SHIFT);
  402. }
  403. static void sprom_extract_r45(struct ssb_sprom *out, const u16 *in)
  404. {
  405. int i;
  406. u16 v;
  407. u16 il0mac_offset;
  408. if (out->revision == 4)
  409. il0mac_offset = SSB_SPROM4_IL0MAC;
  410. else
  411. il0mac_offset = SSB_SPROM5_IL0MAC;
  412. /* extract the MAC address */
  413. for (i = 0; i < 3; i++) {
  414. v = in[SPOFF(il0mac_offset) + i];
  415. *(((__be16 *)out->il0mac) + i) = cpu_to_be16(v);
  416. }
  417. SPEX(et0phyaddr, SSB_SPROM4_ETHPHY, SSB_SPROM4_ETHPHY_ET0A, 0);
  418. SPEX(et1phyaddr, SSB_SPROM4_ETHPHY, SSB_SPROM4_ETHPHY_ET1A,
  419. SSB_SPROM4_ETHPHY_ET1A_SHIFT);
  420. if (out->revision == 4) {
  421. SPEX(country_code, SSB_SPROM4_CCODE, 0xFFFF, 0);
  422. SPEX(boardflags_lo, SSB_SPROM4_BFLLO, 0xFFFF, 0);
  423. SPEX(boardflags_hi, SSB_SPROM4_BFLHI, 0xFFFF, 0);
  424. SPEX(boardflags2_lo, SSB_SPROM4_BFL2LO, 0xFFFF, 0);
  425. SPEX(boardflags2_hi, SSB_SPROM4_BFL2HI, 0xFFFF, 0);
  426. } else {
  427. SPEX(country_code, SSB_SPROM5_CCODE, 0xFFFF, 0);
  428. SPEX(boardflags_lo, SSB_SPROM5_BFLLO, 0xFFFF, 0);
  429. SPEX(boardflags_hi, SSB_SPROM5_BFLHI, 0xFFFF, 0);
  430. SPEX(boardflags2_lo, SSB_SPROM5_BFL2LO, 0xFFFF, 0);
  431. SPEX(boardflags2_hi, SSB_SPROM5_BFL2HI, 0xFFFF, 0);
  432. }
  433. SPEX(ant_available_a, SSB_SPROM4_ANTAVAIL, SSB_SPROM4_ANTAVAIL_A,
  434. SSB_SPROM4_ANTAVAIL_A_SHIFT);
  435. SPEX(ant_available_bg, SSB_SPROM4_ANTAVAIL, SSB_SPROM4_ANTAVAIL_BG,
  436. SSB_SPROM4_ANTAVAIL_BG_SHIFT);
  437. SPEX(maxpwr_bg, SSB_SPROM4_MAXP_BG, SSB_SPROM4_MAXP_BG_MASK, 0);
  438. SPEX(itssi_bg, SSB_SPROM4_MAXP_BG, SSB_SPROM4_ITSSI_BG,
  439. SSB_SPROM4_ITSSI_BG_SHIFT);
  440. SPEX(maxpwr_a, SSB_SPROM4_MAXP_A, SSB_SPROM4_MAXP_A_MASK, 0);
  441. SPEX(itssi_a, SSB_SPROM4_MAXP_A, SSB_SPROM4_ITSSI_A,
  442. SSB_SPROM4_ITSSI_A_SHIFT);
  443. if (out->revision == 4) {
  444. SPEX(gpio0, SSB_SPROM4_GPIOA, SSB_SPROM4_GPIOA_P0, 0);
  445. SPEX(gpio1, SSB_SPROM4_GPIOA, SSB_SPROM4_GPIOA_P1,
  446. SSB_SPROM4_GPIOA_P1_SHIFT);
  447. SPEX(gpio2, SSB_SPROM4_GPIOB, SSB_SPROM4_GPIOB_P2, 0);
  448. SPEX(gpio3, SSB_SPROM4_GPIOB, SSB_SPROM4_GPIOB_P3,
  449. SSB_SPROM4_GPIOB_P3_SHIFT);
  450. } else {
  451. SPEX(gpio0, SSB_SPROM5_GPIOA, SSB_SPROM5_GPIOA_P0, 0);
  452. SPEX(gpio1, SSB_SPROM5_GPIOA, SSB_SPROM5_GPIOA_P1,
  453. SSB_SPROM5_GPIOA_P1_SHIFT);
  454. SPEX(gpio2, SSB_SPROM5_GPIOB, SSB_SPROM5_GPIOB_P2, 0);
  455. SPEX(gpio3, SSB_SPROM5_GPIOB, SSB_SPROM5_GPIOB_P3,
  456. SSB_SPROM5_GPIOB_P3_SHIFT);
  457. }
  458. /* Extract the antenna gain values. */
  459. SPEX(antenna_gain.ghz24.a0, SSB_SPROM4_AGAIN01,
  460. SSB_SPROM4_AGAIN0, SSB_SPROM4_AGAIN0_SHIFT);
  461. SPEX(antenna_gain.ghz24.a1, SSB_SPROM4_AGAIN01,
  462. SSB_SPROM4_AGAIN1, SSB_SPROM4_AGAIN1_SHIFT);
  463. SPEX(antenna_gain.ghz24.a2, SSB_SPROM4_AGAIN23,
  464. SSB_SPROM4_AGAIN2, SSB_SPROM4_AGAIN2_SHIFT);
  465. SPEX(antenna_gain.ghz24.a3, SSB_SPROM4_AGAIN23,
  466. SSB_SPROM4_AGAIN3, SSB_SPROM4_AGAIN3_SHIFT);
  467. memcpy(&out->antenna_gain.ghz5, &out->antenna_gain.ghz24,
  468. sizeof(out->antenna_gain.ghz5));
  469. sprom_extract_r458(out, in);
  470. /* TODO - get remaining rev 4 stuff needed */
  471. }
  472. static void sprom_extract_r8(struct ssb_sprom *out, const u16 *in)
  473. {
  474. int i;
  475. u16 v;
  476. /* extract the MAC address */
  477. for (i = 0; i < 3; i++) {
  478. v = in[SPOFF(SSB_SPROM8_IL0MAC) + i];
  479. *(((__be16 *)out->il0mac) + i) = cpu_to_be16(v);
  480. }
  481. SPEX(country_code, SSB_SPROM8_CCODE, 0xFFFF, 0);
  482. SPEX(boardflags_lo, SSB_SPROM8_BFLLO, 0xFFFF, 0);
  483. SPEX(boardflags_hi, SSB_SPROM8_BFLHI, 0xFFFF, 0);
  484. SPEX(boardflags2_lo, SSB_SPROM8_BFL2LO, 0xFFFF, 0);
  485. SPEX(boardflags2_hi, SSB_SPROM8_BFL2HI, 0xFFFF, 0);
  486. SPEX(ant_available_a, SSB_SPROM8_ANTAVAIL, SSB_SPROM8_ANTAVAIL_A,
  487. SSB_SPROM8_ANTAVAIL_A_SHIFT);
  488. SPEX(ant_available_bg, SSB_SPROM8_ANTAVAIL, SSB_SPROM8_ANTAVAIL_BG,
  489. SSB_SPROM8_ANTAVAIL_BG_SHIFT);
  490. SPEX(maxpwr_bg, SSB_SPROM8_MAXP_BG, SSB_SPROM8_MAXP_BG_MASK, 0);
  491. SPEX(itssi_bg, SSB_SPROM8_MAXP_BG, SSB_SPROM8_ITSSI_BG,
  492. SSB_SPROM8_ITSSI_BG_SHIFT);
  493. SPEX(maxpwr_a, SSB_SPROM8_MAXP_A, SSB_SPROM8_MAXP_A_MASK, 0);
  494. SPEX(itssi_a, SSB_SPROM8_MAXP_A, SSB_SPROM8_ITSSI_A,
  495. SSB_SPROM8_ITSSI_A_SHIFT);
  496. SPEX(maxpwr_ah, SSB_SPROM8_MAXP_AHL, SSB_SPROM8_MAXP_AH_MASK, 0);
  497. SPEX(maxpwr_al, SSB_SPROM8_MAXP_AHL, SSB_SPROM8_MAXP_AL_MASK,
  498. SSB_SPROM8_MAXP_AL_SHIFT);
  499. SPEX(gpio0, SSB_SPROM8_GPIOA, SSB_SPROM8_GPIOA_P0, 0);
  500. SPEX(gpio1, SSB_SPROM8_GPIOA, SSB_SPROM8_GPIOA_P1,
  501. SSB_SPROM8_GPIOA_P1_SHIFT);
  502. SPEX(gpio2, SSB_SPROM8_GPIOB, SSB_SPROM8_GPIOB_P2, 0);
  503. SPEX(gpio3, SSB_SPROM8_GPIOB, SSB_SPROM8_GPIOB_P3,
  504. SSB_SPROM8_GPIOB_P3_SHIFT);
  505. SPEX(tri2g, SSB_SPROM8_TRI25G, SSB_SPROM8_TRI2G, 0);
  506. SPEX(tri5g, SSB_SPROM8_TRI25G, SSB_SPROM8_TRI5G,
  507. SSB_SPROM8_TRI5G_SHIFT);
  508. SPEX(tri5gl, SSB_SPROM8_TRI5GHL, SSB_SPROM8_TRI5GL, 0);
  509. SPEX(tri5gh, SSB_SPROM8_TRI5GHL, SSB_SPROM8_TRI5GH,
  510. SSB_SPROM8_TRI5GH_SHIFT);
  511. SPEX(rxpo2g, SSB_SPROM8_RXPO, SSB_SPROM8_RXPO2G, 0);
  512. SPEX(rxpo5g, SSB_SPROM8_RXPO, SSB_SPROM8_RXPO5G,
  513. SSB_SPROM8_RXPO5G_SHIFT);
  514. SPEX(rssismf2g, SSB_SPROM8_RSSIPARM2G, SSB_SPROM8_RSSISMF2G, 0);
  515. SPEX(rssismc2g, SSB_SPROM8_RSSIPARM2G, SSB_SPROM8_RSSISMC2G,
  516. SSB_SPROM8_RSSISMC2G_SHIFT);
  517. SPEX(rssisav2g, SSB_SPROM8_RSSIPARM2G, SSB_SPROM8_RSSISAV2G,
  518. SSB_SPROM8_RSSISAV2G_SHIFT);
  519. SPEX(bxa2g, SSB_SPROM8_RSSIPARM2G, SSB_SPROM8_BXA2G,
  520. SSB_SPROM8_BXA2G_SHIFT);
  521. SPEX(rssismf5g, SSB_SPROM8_RSSIPARM5G, SSB_SPROM8_RSSISMF5G, 0);
  522. SPEX(rssismc5g, SSB_SPROM8_RSSIPARM5G, SSB_SPROM8_RSSISMC5G,
  523. SSB_SPROM8_RSSISMC5G_SHIFT);
  524. SPEX(rssisav5g, SSB_SPROM8_RSSIPARM5G, SSB_SPROM8_RSSISAV5G,
  525. SSB_SPROM8_RSSISAV5G_SHIFT);
  526. SPEX(bxa5g, SSB_SPROM8_RSSIPARM5G, SSB_SPROM8_BXA5G,
  527. SSB_SPROM8_BXA5G_SHIFT);
  528. SPEX(pa0b0, SSB_SPROM8_PA0B0, 0xFFFF, 0);
  529. SPEX(pa0b1, SSB_SPROM8_PA0B1, 0xFFFF, 0);
  530. SPEX(pa0b2, SSB_SPROM8_PA0B2, 0xFFFF, 0);
  531. SPEX(pa1b0, SSB_SPROM8_PA1B0, 0xFFFF, 0);
  532. SPEX(pa1b1, SSB_SPROM8_PA1B1, 0xFFFF, 0);
  533. SPEX(pa1b2, SSB_SPROM8_PA1B2, 0xFFFF, 0);
  534. SPEX(pa1lob0, SSB_SPROM8_PA1LOB0, 0xFFFF, 0);
  535. SPEX(pa1lob1, SSB_SPROM8_PA1LOB1, 0xFFFF, 0);
  536. SPEX(pa1lob2, SSB_SPROM8_PA1LOB2, 0xFFFF, 0);
  537. SPEX(pa1hib0, SSB_SPROM8_PA1HIB0, 0xFFFF, 0);
  538. SPEX(pa1hib1, SSB_SPROM8_PA1HIB1, 0xFFFF, 0);
  539. SPEX(pa1hib2, SSB_SPROM8_PA1HIB2, 0xFFFF, 0);
  540. SPEX(cck2gpo, SSB_SPROM8_CCK2GPO, 0xFFFF, 0);
  541. SPEX32(ofdm2gpo, SSB_SPROM8_OFDM2GPO, 0xFFFFFFFF, 0);
  542. SPEX32(ofdm5glpo, SSB_SPROM8_OFDM5GLPO, 0xFFFFFFFF, 0);
  543. SPEX32(ofdm5gpo, SSB_SPROM8_OFDM5GPO, 0xFFFFFFFF, 0);
  544. SPEX32(ofdm5ghpo, SSB_SPROM8_OFDM5GHPO, 0xFFFFFFFF, 0);
  545. /* Extract the antenna gain values. */
  546. SPEX(antenna_gain.ghz24.a0, SSB_SPROM8_AGAIN01,
  547. SSB_SPROM8_AGAIN0, SSB_SPROM8_AGAIN0_SHIFT);
  548. SPEX(antenna_gain.ghz24.a1, SSB_SPROM8_AGAIN01,
  549. SSB_SPROM8_AGAIN1, SSB_SPROM8_AGAIN1_SHIFT);
  550. SPEX(antenna_gain.ghz24.a2, SSB_SPROM8_AGAIN23,
  551. SSB_SPROM8_AGAIN2, SSB_SPROM8_AGAIN2_SHIFT);
  552. SPEX(antenna_gain.ghz24.a3, SSB_SPROM8_AGAIN23,
  553. SSB_SPROM8_AGAIN3, SSB_SPROM8_AGAIN3_SHIFT);
  554. memcpy(&out->antenna_gain.ghz5, &out->antenna_gain.ghz24,
  555. sizeof(out->antenna_gain.ghz5));
  556. sprom_extract_r458(out, in);
  557. /* TODO - get remaining rev 8 stuff needed */
  558. }
  559. static int sprom_extract(struct ssb_bus *bus, struct ssb_sprom *out,
  560. const u16 *in, u16 size)
  561. {
  562. memset(out, 0, sizeof(*out));
  563. out->revision = in[size - 1] & 0x00FF;
  564. ssb_dprintk(KERN_DEBUG PFX "SPROM revision %d detected.\n", out->revision);
  565. memset(out->et0mac, 0xFF, 6); /* preset et0 and et1 mac */
  566. memset(out->et1mac, 0xFF, 6);
  567. if ((bus->chip_id & 0xFF00) == 0x4400) {
  568. /* Workaround: The BCM44XX chip has a stupid revision
  569. * number stored in the SPROM.
  570. * Always extract r1. */
  571. out->revision = 1;
  572. ssb_dprintk(KERN_DEBUG PFX "SPROM treated as revision %d\n", out->revision);
  573. }
  574. switch (out->revision) {
  575. case 1:
  576. case 2:
  577. case 3:
  578. sprom_extract_r123(out, in);
  579. break;
  580. case 4:
  581. case 5:
  582. sprom_extract_r45(out, in);
  583. break;
  584. case 8:
  585. sprom_extract_r8(out, in);
  586. break;
  587. default:
  588. ssb_printk(KERN_WARNING PFX "Unsupported SPROM"
  589. " revision %d detected. Will extract"
  590. " v1\n", out->revision);
  591. out->revision = 1;
  592. sprom_extract_r123(out, in);
  593. }
  594. if (out->boardflags_lo == 0xFFFF)
  595. out->boardflags_lo = 0; /* per specs */
  596. if (out->boardflags_hi == 0xFFFF)
  597. out->boardflags_hi = 0; /* per specs */
  598. return 0;
  599. }
  600. static int ssb_pci_sprom_get(struct ssb_bus *bus,
  601. struct ssb_sprom *sprom)
  602. {
  603. int err;
  604. u16 *buf;
  605. if (!ssb_is_sprom_available(bus)) {
  606. ssb_printk(KERN_ERR PFX "No SPROM available!\n");
  607. return -ENODEV;
  608. }
  609. if (bus->chipco.dev) { /* can be unavailable! */
  610. /*
  611. * get SPROM offset: SSB_SPROM_BASE1 except for
  612. * chipcommon rev >= 31 or chip ID is 0x4312 and
  613. * chipcommon status & 3 == 2
  614. */
  615. if (bus->chipco.dev->id.revision >= 31)
  616. bus->sprom_offset = SSB_SPROM_BASE31;
  617. else if (bus->chip_id == 0x4312 &&
  618. (bus->chipco.status & 0x03) == 2)
  619. bus->sprom_offset = SSB_SPROM_BASE31;
  620. else
  621. bus->sprom_offset = SSB_SPROM_BASE1;
  622. } else {
  623. bus->sprom_offset = SSB_SPROM_BASE1;
  624. }
  625. ssb_dprintk(KERN_INFO PFX "SPROM offset is 0x%x\n", bus->sprom_offset);
  626. buf = kcalloc(SSB_SPROMSIZE_WORDS_R123, sizeof(u16), GFP_KERNEL);
  627. if (!buf)
  628. return -ENOMEM;
  629. bus->sprom_size = SSB_SPROMSIZE_WORDS_R123;
  630. sprom_do_read(bus, buf);
  631. err = sprom_check_crc(buf, bus->sprom_size);
  632. if (err) {
  633. /* try for a 440 byte SPROM - revision 4 and higher */
  634. kfree(buf);
  635. buf = kcalloc(SSB_SPROMSIZE_WORDS_R4, sizeof(u16),
  636. GFP_KERNEL);
  637. if (!buf)
  638. return -ENOMEM;
  639. bus->sprom_size = SSB_SPROMSIZE_WORDS_R4;
  640. sprom_do_read(bus, buf);
  641. err = sprom_check_crc(buf, bus->sprom_size);
  642. if (err) {
  643. /* All CRC attempts failed.
  644. * Maybe there is no SPROM on the device?
  645. * Now we ask the arch code if there is some sprom
  646. * available for this device in some other storage */
  647. err = ssb_fill_sprom_with_fallback(bus, sprom);
  648. if (err) {
  649. ssb_printk(KERN_WARNING PFX "WARNING: Using"
  650. " fallback SPROM failed (err %d)\n",
  651. err);
  652. } else {
  653. ssb_dprintk(KERN_DEBUG PFX "Using SPROM"
  654. " revision %d provided by"
  655. " platform.\n", sprom->revision);
  656. err = 0;
  657. goto out_free;
  658. }
  659. ssb_printk(KERN_WARNING PFX "WARNING: Invalid"
  660. " SPROM CRC (corrupt SPROM)\n");
  661. }
  662. }
  663. err = sprom_extract(bus, sprom, buf, bus->sprom_size);
  664. out_free:
  665. kfree(buf);
  666. return err;
  667. }
  668. static void ssb_pci_get_boardinfo(struct ssb_bus *bus,
  669. struct ssb_boardinfo *bi)
  670. {
  671. pci_read_config_word(bus->host_pci, PCI_SUBSYSTEM_VENDOR_ID,
  672. &bi->vendor);
  673. pci_read_config_word(bus->host_pci, PCI_SUBSYSTEM_ID,
  674. &bi->type);
  675. pci_read_config_word(bus->host_pci, PCI_REVISION_ID,
  676. &bi->rev);
  677. }
  678. int ssb_pci_get_invariants(struct ssb_bus *bus,
  679. struct ssb_init_invariants *iv)
  680. {
  681. int err;
  682. err = ssb_pci_sprom_get(bus, &iv->sprom);
  683. if (err)
  684. goto out;
  685. ssb_pci_get_boardinfo(bus, &iv->boardinfo);
  686. out:
  687. return err;
  688. }
  689. #ifdef CONFIG_SSB_DEBUG
  690. static int ssb_pci_assert_buspower(struct ssb_bus *bus)
  691. {
  692. if (likely(bus->powered_up))
  693. return 0;
  694. printk(KERN_ERR PFX "FATAL ERROR: Bus powered down "
  695. "while accessing PCI MMIO space\n");
  696. if (bus->power_warn_count <= 10) {
  697. bus->power_warn_count++;
  698. dump_stack();
  699. }
  700. return -ENODEV;
  701. }
  702. #else /* DEBUG */
  703. static inline int ssb_pci_assert_buspower(struct ssb_bus *bus)
  704. {
  705. return 0;
  706. }
  707. #endif /* DEBUG */
  708. static u8 ssb_pci_read8(struct ssb_device *dev, u16 offset)
  709. {
  710. struct ssb_bus *bus = dev->bus;
  711. if (unlikely(ssb_pci_assert_buspower(bus)))
  712. return 0xFF;
  713. if (unlikely(bus->mapped_device != dev)) {
  714. if (unlikely(ssb_pci_switch_core(bus, dev)))
  715. return 0xFF;
  716. }
  717. return ioread8(bus->mmio + offset);
  718. }
  719. static u16 ssb_pci_read16(struct ssb_device *dev, u16 offset)
  720. {
  721. struct ssb_bus *bus = dev->bus;
  722. if (unlikely(ssb_pci_assert_buspower(bus)))
  723. return 0xFFFF;
  724. if (unlikely(bus->mapped_device != dev)) {
  725. if (unlikely(ssb_pci_switch_core(bus, dev)))
  726. return 0xFFFF;
  727. }
  728. return ioread16(bus->mmio + offset);
  729. }
  730. static u32 ssb_pci_read32(struct ssb_device *dev, u16 offset)
  731. {
  732. struct ssb_bus *bus = dev->bus;
  733. if (unlikely(ssb_pci_assert_buspower(bus)))
  734. return 0xFFFFFFFF;
  735. if (unlikely(bus->mapped_device != dev)) {
  736. if (unlikely(ssb_pci_switch_core(bus, dev)))
  737. return 0xFFFFFFFF;
  738. }
  739. return ioread32(bus->mmio + offset);
  740. }
  741. #ifdef CONFIG_SSB_BLOCKIO
  742. static void ssb_pci_block_read(struct ssb_device *dev, void *buffer,
  743. size_t count, u16 offset, u8 reg_width)
  744. {
  745. struct ssb_bus *bus = dev->bus;
  746. void __iomem *addr = bus->mmio + offset;
  747. if (unlikely(ssb_pci_assert_buspower(bus)))
  748. goto error;
  749. if (unlikely(bus->mapped_device != dev)) {
  750. if (unlikely(ssb_pci_switch_core(bus, dev)))
  751. goto error;
  752. }
  753. switch (reg_width) {
  754. case sizeof(u8):
  755. ioread8_rep(addr, buffer, count);
  756. break;
  757. case sizeof(u16):
  758. SSB_WARN_ON(count & 1);
  759. ioread16_rep(addr, buffer, count >> 1);
  760. break;
  761. case sizeof(u32):
  762. SSB_WARN_ON(count & 3);
  763. ioread32_rep(addr, buffer, count >> 2);
  764. break;
  765. default:
  766. SSB_WARN_ON(1);
  767. }
  768. return;
  769. error:
  770. memset(buffer, 0xFF, count);
  771. }
  772. #endif /* CONFIG_SSB_BLOCKIO */
  773. static void ssb_pci_write8(struct ssb_device *dev, u16 offset, u8 value)
  774. {
  775. struct ssb_bus *bus = dev->bus;
  776. if (unlikely(ssb_pci_assert_buspower(bus)))
  777. return;
  778. if (unlikely(bus->mapped_device != dev)) {
  779. if (unlikely(ssb_pci_switch_core(bus, dev)))
  780. return;
  781. }
  782. iowrite8(value, bus->mmio + offset);
  783. }
  784. static void ssb_pci_write16(struct ssb_device *dev, u16 offset, u16 value)
  785. {
  786. struct ssb_bus *bus = dev->bus;
  787. if (unlikely(ssb_pci_assert_buspower(bus)))
  788. return;
  789. if (unlikely(bus->mapped_device != dev)) {
  790. if (unlikely(ssb_pci_switch_core(bus, dev)))
  791. return;
  792. }
  793. iowrite16(value, bus->mmio + offset);
  794. }
  795. static void ssb_pci_write32(struct ssb_device *dev, u16 offset, u32 value)
  796. {
  797. struct ssb_bus *bus = dev->bus;
  798. if (unlikely(ssb_pci_assert_buspower(bus)))
  799. return;
  800. if (unlikely(bus->mapped_device != dev)) {
  801. if (unlikely(ssb_pci_switch_core(bus, dev)))
  802. return;
  803. }
  804. iowrite32(value, bus->mmio + offset);
  805. }
  806. #ifdef CONFIG_SSB_BLOCKIO
  807. static void ssb_pci_block_write(struct ssb_device *dev, const void *buffer,
  808. size_t count, u16 offset, u8 reg_width)
  809. {
  810. struct ssb_bus *bus = dev->bus;
  811. void __iomem *addr = bus->mmio + offset;
  812. if (unlikely(ssb_pci_assert_buspower(bus)))
  813. return;
  814. if (unlikely(bus->mapped_device != dev)) {
  815. if (unlikely(ssb_pci_switch_core(bus, dev)))
  816. return;
  817. }
  818. switch (reg_width) {
  819. case sizeof(u8):
  820. iowrite8_rep(addr, buffer, count);
  821. break;
  822. case sizeof(u16):
  823. SSB_WARN_ON(count & 1);
  824. iowrite16_rep(addr, buffer, count >> 1);
  825. break;
  826. case sizeof(u32):
  827. SSB_WARN_ON(count & 3);
  828. iowrite32_rep(addr, buffer, count >> 2);
  829. break;
  830. default:
  831. SSB_WARN_ON(1);
  832. }
  833. }
  834. #endif /* CONFIG_SSB_BLOCKIO */
  835. /* Not "static", as it's used in main.c */
  836. const struct ssb_bus_ops ssb_pci_ops = {
  837. .read8 = ssb_pci_read8,
  838. .read16 = ssb_pci_read16,
  839. .read32 = ssb_pci_read32,
  840. .write8 = ssb_pci_write8,
  841. .write16 = ssb_pci_write16,
  842. .write32 = ssb_pci_write32,
  843. #ifdef CONFIG_SSB_BLOCKIO
  844. .block_read = ssb_pci_block_read,
  845. .block_write = ssb_pci_block_write,
  846. #endif
  847. };
  848. static ssize_t ssb_pci_attr_sprom_show(struct device *pcidev,
  849. struct device_attribute *attr,
  850. char *buf)
  851. {
  852. struct pci_dev *pdev = container_of(pcidev, struct pci_dev, dev);
  853. struct ssb_bus *bus;
  854. bus = ssb_pci_dev_to_bus(pdev);
  855. if (!bus)
  856. return -ENODEV;
  857. return ssb_attr_sprom_show(bus, buf, sprom_do_read);
  858. }
  859. static ssize_t ssb_pci_attr_sprom_store(struct device *pcidev,
  860. struct device_attribute *attr,
  861. const char *buf, size_t count)
  862. {
  863. struct pci_dev *pdev = container_of(pcidev, struct pci_dev, dev);
  864. struct ssb_bus *bus;
  865. bus = ssb_pci_dev_to_bus(pdev);
  866. if (!bus)
  867. return -ENODEV;
  868. return ssb_attr_sprom_store(bus, buf, count,
  869. sprom_check_crc, sprom_do_write);
  870. }
  871. static DEVICE_ATTR(ssb_sprom, 0600,
  872. ssb_pci_attr_sprom_show,
  873. ssb_pci_attr_sprom_store);
  874. void ssb_pci_exit(struct ssb_bus *bus)
  875. {
  876. struct pci_dev *pdev;
  877. if (bus->bustype != SSB_BUSTYPE_PCI)
  878. return;
  879. pdev = bus->host_pci;
  880. device_remove_file(&pdev->dev, &dev_attr_ssb_sprom);
  881. }
  882. int ssb_pci_init(struct ssb_bus *bus)
  883. {
  884. struct pci_dev *pdev;
  885. int err;
  886. if (bus->bustype != SSB_BUSTYPE_PCI)
  887. return 0;
  888. pdev = bus->host_pci;
  889. mutex_init(&bus->sprom_mutex);
  890. err = device_create_file(&pdev->dev, &dev_attr_ssb_sprom);
  891. if (err)
  892. goto out;
  893. out:
  894. return err;
  895. }