core_marvel.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/alpha/kernel/core_marvel.c
  4. *
  5. * Code common to all Marvel based systems.
  6. */
  7. #define __EXTERN_INLINE inline
  8. #include <asm/io.h>
  9. #include <asm/core_marvel.h>
  10. #undef __EXTERN_INLINE
  11. #include <linux/types.h>
  12. #include <linux/pci.h>
  13. #include <linux/sched.h>
  14. #include <linux/init.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/mc146818rtc.h>
  17. #include <linux/rtc.h>
  18. #include <linux/module.h>
  19. #include <linux/memblock.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/smp.h>
  22. #include <asm/gct.h>
  23. #include <asm/pgalloc.h>
  24. #include <asm/tlbflush.h>
  25. #include <asm/vga.h>
  26. #include "proto.h"
  27. #include "pci_impl.h"
  28. /*
  29. * Debug helpers
  30. */
  31. #define DEBUG_CONFIG 0
  32. #if DEBUG_CONFIG
  33. # define DBG_CFG(args) printk args
  34. #else
  35. # define DBG_CFG(args)
  36. #endif
  37. /*
  38. * Private data
  39. */
  40. static struct io7 *io7_head = NULL;
  41. /*
  42. * Helper functions
  43. */
  44. static unsigned long __attribute__ ((unused))
  45. read_ev7_csr(int pe, unsigned long offset)
  46. {
  47. ev7_csr *ev7csr = EV7_CSR_KERN(pe, offset);
  48. unsigned long q;
  49. mb();
  50. q = ev7csr->csr;
  51. mb();
  52. return q;
  53. }
  54. static void __attribute__ ((unused))
  55. write_ev7_csr(int pe, unsigned long offset, unsigned long q)
  56. {
  57. ev7_csr *ev7csr = EV7_CSR_KERN(pe, offset);
  58. mb();
  59. ev7csr->csr = q;
  60. mb();
  61. }
  62. static char * __init
  63. mk_resource_name(int pe, int port, char *str)
  64. {
  65. char tmp[80];
  66. char *name;
  67. sprintf(tmp, "PCI %s PE %d PORT %d", str, pe, port);
  68. name = memblock_alloc(strlen(tmp) + 1, SMP_CACHE_BYTES);
  69. strcpy(name, tmp);
  70. return name;
  71. }
  72. inline struct io7 *
  73. marvel_next_io7(struct io7 *prev)
  74. {
  75. return (prev ? prev->next : io7_head);
  76. }
  77. struct io7 *
  78. marvel_find_io7(int pe)
  79. {
  80. struct io7 *io7;
  81. for (io7 = io7_head; io7 && io7->pe != pe; io7 = io7->next)
  82. continue;
  83. return io7;
  84. }
  85. static struct io7 * __init
  86. alloc_io7(unsigned int pe)
  87. {
  88. struct io7 *io7;
  89. struct io7 *insp;
  90. int h;
  91. if (marvel_find_io7(pe)) {
  92. printk(KERN_WARNING "IO7 at PE %d already allocated!\n", pe);
  93. return NULL;
  94. }
  95. io7 = memblock_alloc(sizeof(*io7), SMP_CACHE_BYTES);
  96. io7->pe = pe;
  97. raw_spin_lock_init(&io7->irq_lock);
  98. for (h = 0; h < 4; h++) {
  99. io7->ports[h].io7 = io7;
  100. io7->ports[h].port = h;
  101. io7->ports[h].enabled = 0; /* default to disabled */
  102. }
  103. /*
  104. * Insert in pe sorted order.
  105. */
  106. if (NULL == io7_head) /* empty list */
  107. io7_head = io7;
  108. else if (io7_head->pe > io7->pe) { /* insert at head */
  109. io7->next = io7_head;
  110. io7_head = io7;
  111. } else { /* insert at position */
  112. for (insp = io7_head; insp; insp = insp->next) {
  113. if (insp->pe == io7->pe) {
  114. printk(KERN_ERR "Too many IO7s at PE %d\n",
  115. io7->pe);
  116. return NULL;
  117. }
  118. if (NULL == insp->next ||
  119. insp->next->pe > io7->pe) { /* insert here */
  120. io7->next = insp->next;
  121. insp->next = io7;
  122. break;
  123. }
  124. }
  125. if (NULL == insp) { /* couldn't insert ?!? */
  126. printk(KERN_WARNING "Failed to insert IO7 at PE %d "
  127. " - adding at head of list\n", io7->pe);
  128. io7->next = io7_head;
  129. io7_head = io7;
  130. }
  131. }
  132. return io7;
  133. }
  134. void
  135. io7_clear_errors(struct io7 *io7)
  136. {
  137. io7_port7_csrs *p7csrs;
  138. io7_ioport_csrs *csrs;
  139. int port;
  140. /*
  141. * First the IO ports.
  142. */
  143. for (port = 0; port < 4; port++) {
  144. csrs = IO7_CSRS_KERN(io7->pe, port);
  145. csrs->POx_ERR_SUM.csr = -1UL;
  146. csrs->POx_TLB_ERR.csr = -1UL;
  147. csrs->POx_SPL_COMPLT.csr = -1UL;
  148. csrs->POx_TRANS_SUM.csr = -1UL;
  149. }
  150. /*
  151. * Then the common ones.
  152. */
  153. p7csrs = IO7_PORT7_CSRS_KERN(io7->pe);
  154. p7csrs->PO7_ERROR_SUM.csr = -1UL;
  155. p7csrs->PO7_UNCRR_SYM.csr = -1UL;
  156. p7csrs->PO7_CRRCT_SYM.csr = -1UL;
  157. }
  158. /*
  159. * IO7 PCI, PCI/X, AGP configuration.
  160. */
  161. static void __init
  162. io7_init_hose(struct io7 *io7, int port)
  163. {
  164. static int hose_index = 0;
  165. struct pci_controller *hose = alloc_pci_controller();
  166. struct io7_port *io7_port = &io7->ports[port];
  167. io7_ioport_csrs *csrs = IO7_CSRS_KERN(io7->pe, port);
  168. int i;
  169. hose->index = hose_index++; /* arbitrary */
  170. /*
  171. * We don't have an isa or legacy hose, but glibc expects to be
  172. * able to use the bus == 0 / dev == 0 form of the iobase syscall
  173. * to determine information about the i/o system. Since XFree86
  174. * relies on glibc's determination to tell whether or not to use
  175. * sparse access, we need to point the pci_isa_hose at a real hose
  176. * so at least that determination is correct.
  177. */
  178. if (hose->index == 0)
  179. pci_isa_hose = hose;
  180. io7_port->csrs = csrs;
  181. io7_port->hose = hose;
  182. hose->sysdata = io7_port;
  183. hose->io_space = alloc_resource();
  184. hose->mem_space = alloc_resource();
  185. /*
  186. * Base addresses for userland consumption. Since these are going
  187. * to be mapped, they are pure physical addresses.
  188. */
  189. hose->sparse_mem_base = hose->sparse_io_base = 0;
  190. hose->dense_mem_base = IO7_MEM_PHYS(io7->pe, port);
  191. hose->dense_io_base = IO7_IO_PHYS(io7->pe, port);
  192. /*
  193. * Base addresses and resource ranges for kernel consumption.
  194. */
  195. hose->config_space_base = (unsigned long)IO7_CONF_KERN(io7->pe, port);
  196. hose->io_space->start = (unsigned long)IO7_IO_KERN(io7->pe, port);
  197. hose->io_space->end = hose->io_space->start + IO7_IO_SPACE - 1;
  198. hose->io_space->name = mk_resource_name(io7->pe, port, "IO");
  199. hose->io_space->flags = IORESOURCE_IO;
  200. hose->mem_space->start = (unsigned long)IO7_MEM_KERN(io7->pe, port);
  201. hose->mem_space->end = hose->mem_space->start + IO7_MEM_SPACE - 1;
  202. hose->mem_space->name = mk_resource_name(io7->pe, port, "MEM");
  203. hose->mem_space->flags = IORESOURCE_MEM;
  204. if (request_resource(&ioport_resource, hose->io_space) < 0)
  205. printk(KERN_ERR "Failed to request IO on hose %d\n",
  206. hose->index);
  207. if (request_resource(&iomem_resource, hose->mem_space) < 0)
  208. printk(KERN_ERR "Failed to request MEM on hose %d\n",
  209. hose->index);
  210. /*
  211. * Save the existing DMA window settings for later restoration.
  212. */
  213. for (i = 0; i < 4; i++) {
  214. io7_port->saved_wbase[i] = csrs->POx_WBASE[i].csr;
  215. io7_port->saved_wmask[i] = csrs->POx_WMASK[i].csr;
  216. io7_port->saved_tbase[i] = csrs->POx_TBASE[i].csr;
  217. }
  218. /*
  219. * Set up the PCI to main memory translation windows.
  220. *
  221. * Window 0 is scatter-gather 8MB at 8MB
  222. * Window 1 is direct access 1GB at 2GB
  223. * Window 2 is scatter-gather (up-to) 1GB at 3GB
  224. * Window 3 is disabled
  225. */
  226. /*
  227. * TBIA before modifying windows.
  228. */
  229. marvel_pci_tbi(hose, 0, -1);
  230. /*
  231. * Set up window 0 for scatter-gather 8MB at 8MB.
  232. */
  233. hose->sg_isa = iommu_arena_new_node(marvel_cpuid_to_nid(io7->pe),
  234. hose, 0x00800000, 0x00800000, 0);
  235. hose->sg_isa->align_entry = 8; /* cache line boundary */
  236. csrs->POx_WBASE[0].csr =
  237. hose->sg_isa->dma_base | wbase_m_ena | wbase_m_sg;
  238. csrs->POx_WMASK[0].csr = (hose->sg_isa->size - 1) & wbase_m_addr;
  239. csrs->POx_TBASE[0].csr = virt_to_phys(hose->sg_isa->ptes);
  240. /*
  241. * Set up window 1 for direct-mapped 1GB at 2GB.
  242. */
  243. csrs->POx_WBASE[1].csr = __direct_map_base | wbase_m_ena;
  244. csrs->POx_WMASK[1].csr = (__direct_map_size - 1) & wbase_m_addr;
  245. csrs->POx_TBASE[1].csr = 0;
  246. /*
  247. * Set up window 2 for scatter-gather (up-to) 1GB at 3GB.
  248. */
  249. hose->sg_pci = iommu_arena_new_node(marvel_cpuid_to_nid(io7->pe),
  250. hose, 0xc0000000, 0x40000000, 0);
  251. hose->sg_pci->align_entry = 8; /* cache line boundary */
  252. csrs->POx_WBASE[2].csr =
  253. hose->sg_pci->dma_base | wbase_m_ena | wbase_m_sg;
  254. csrs->POx_WMASK[2].csr = (hose->sg_pci->size - 1) & wbase_m_addr;
  255. csrs->POx_TBASE[2].csr = virt_to_phys(hose->sg_pci->ptes);
  256. /*
  257. * Disable window 3.
  258. */
  259. csrs->POx_WBASE[3].csr = 0;
  260. /*
  261. * Make sure that the AGP Monster Window is disabled.
  262. */
  263. csrs->POx_CTRL.csr &= ~(1UL << 61);
  264. #if 1
  265. printk("FIXME: disabling master aborts\n");
  266. csrs->POx_MSK_HEI.csr &= ~(3UL << 14);
  267. #endif
  268. /*
  269. * TBIA after modifying windows.
  270. */
  271. marvel_pci_tbi(hose, 0, -1);
  272. }
  273. static void __init
  274. marvel_init_io7(struct io7 *io7)
  275. {
  276. int i;
  277. printk("Initializing IO7 at PID %d\n", io7->pe);
  278. /*
  279. * Get the Port 7 CSR pointer.
  280. */
  281. io7->csrs = IO7_PORT7_CSRS_KERN(io7->pe);
  282. /*
  283. * Init this IO7's hoses.
  284. */
  285. for (i = 0; i < IO7_NUM_PORTS; i++) {
  286. io7_ioport_csrs *csrs = IO7_CSRS_KERN(io7->pe, i);
  287. if (csrs->POx_CACHE_CTL.csr == 8) {
  288. io7->ports[i].enabled = 1;
  289. io7_init_hose(io7, i);
  290. }
  291. }
  292. }
  293. void __init
  294. marvel_io7_present(gct6_node *node)
  295. {
  296. int pe;
  297. if (node->type != GCT_TYPE_HOSE ||
  298. node->subtype != GCT_SUBTYPE_IO_PORT_MODULE)
  299. return;
  300. pe = (node->id >> 8) & 0xff;
  301. printk("Found an IO7 at PID %d\n", pe);
  302. alloc_io7(pe);
  303. }
  304. static void __init
  305. marvel_find_console_vga_hose(void)
  306. {
  307. #ifdef CONFIG_VGA_HOSE
  308. u64 *pu64 = (u64 *)((u64)hwrpb + hwrpb->ctbt_offset);
  309. if (pu64[7] == 3) { /* TERM_TYPE == graphics */
  310. struct pci_controller *hose = NULL;
  311. int h = (pu64[30] >> 24) & 0xff; /* TERM_OUT_LOC, hose # */
  312. struct io7 *io7;
  313. int pid, port;
  314. /* FIXME - encoding is going to have to change for Marvel
  315. * since hose will be able to overflow a byte...
  316. * need to fix this decode when the console
  317. * changes its encoding
  318. */
  319. printk("console graphics is on hose %d (console)\n", h);
  320. /*
  321. * The console's hose numbering is:
  322. *
  323. * hose<n:2>: PID
  324. * hose<1:0>: PORT
  325. *
  326. * We need to find the hose at that pid and port
  327. */
  328. pid = h >> 2;
  329. port = h & 3;
  330. if ((io7 = marvel_find_io7(pid)))
  331. hose = io7->ports[port].hose;
  332. if (hose) {
  333. printk("Console graphics on hose %d\n", hose->index);
  334. pci_vga_hose = hose;
  335. }
  336. }
  337. #endif
  338. }
  339. gct6_search_struct gct_wanted_node_list[] __initdata = {
  340. { GCT_TYPE_HOSE, GCT_SUBTYPE_IO_PORT_MODULE, marvel_io7_present },
  341. { 0, 0, NULL }
  342. };
  343. /*
  344. * In case the GCT is not complete, let the user specify PIDs with IO7s
  345. * at boot time. Syntax is 'io7=a,b,c,...,n' where a-n are the PIDs (decimal)
  346. * where IO7s are connected
  347. */
  348. static int __init
  349. marvel_specify_io7(char *str)
  350. {
  351. unsigned long pid;
  352. struct io7 *io7;
  353. char *pchar;
  354. do {
  355. pid = simple_strtoul(str, &pchar, 0);
  356. if (pchar != str) {
  357. printk("User-specified IO7 at PID %lu\n", pid);
  358. io7 = alloc_io7(pid);
  359. if (io7) marvel_init_io7(io7);
  360. }
  361. if (pchar == str) pchar++;
  362. str = pchar;
  363. } while(*str);
  364. return 1;
  365. }
  366. __setup("io7=", marvel_specify_io7);
  367. void __init
  368. marvel_init_arch(void)
  369. {
  370. struct io7 *io7;
  371. /* With multiple PCI busses, we play with I/O as physical addrs. */
  372. ioport_resource.end = ~0UL;
  373. /* PCI DMA Direct Mapping is 1GB at 2GB. */
  374. __direct_map_base = 0x80000000;
  375. __direct_map_size = 0x40000000;
  376. /* Parse the config tree. */
  377. gct6_find_nodes(GCT_NODE_PTR(0), gct_wanted_node_list);
  378. /* Init the io7s. */
  379. for (io7 = NULL; NULL != (io7 = marvel_next_io7(io7)); )
  380. marvel_init_io7(io7);
  381. /* Check for graphic console location (if any). */
  382. marvel_find_console_vga_hose();
  383. }
  384. void
  385. marvel_kill_arch(int mode)
  386. {
  387. }
  388. /*
  389. * PCI Configuration Space access functions
  390. *
  391. * Configuration space addresses have the following format:
  392. *
  393. * |2 2 2 2|1 1 1 1|1 1 1 1|1 1
  394. * |3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
  395. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  396. * |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|R|R|
  397. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  398. *
  399. * n:24 reserved for hose base
  400. * 23:16 bus number (8 bits = 128 possible buses)
  401. * 15:11 Device number (5 bits)
  402. * 10:8 function number
  403. * 7:2 register number
  404. *
  405. * Notes:
  406. * IO7 determines whether to use a type 0 or type 1 config cycle
  407. * based on the bus number. Therefore the bus number must be set
  408. * to 0 for the root bus on any hose.
  409. *
  410. * The function number selects which function of a multi-function device
  411. * (e.g., SCSI and Ethernet).
  412. *
  413. */
  414. static inline unsigned long
  415. build_conf_addr(struct pci_controller *hose, u8 bus,
  416. unsigned int devfn, int where)
  417. {
  418. return (hose->config_space_base | (bus << 16) | (devfn << 8) | where);
  419. }
  420. static unsigned long
  421. mk_conf_addr(struct pci_bus *pbus, unsigned int devfn, int where)
  422. {
  423. struct pci_controller *hose = pbus->sysdata;
  424. struct io7_port *io7_port;
  425. unsigned long addr = 0;
  426. u8 bus = pbus->number;
  427. if (!hose)
  428. return addr;
  429. /* Check for enabled. */
  430. io7_port = hose->sysdata;
  431. if (!io7_port->enabled)
  432. return addr;
  433. if (!pbus->parent) { /* No parent means peer PCI bus. */
  434. /* Don't support idsel > 20 on primary bus. */
  435. if (devfn >= PCI_DEVFN(21, 0))
  436. return addr;
  437. bus = 0;
  438. }
  439. addr = build_conf_addr(hose, bus, devfn, where);
  440. DBG_CFG(("mk_conf_addr: returning pci_addr 0x%lx\n", addr));
  441. return addr;
  442. }
  443. static int
  444. marvel_read_config(struct pci_bus *bus, unsigned int devfn, int where,
  445. int size, u32 *value)
  446. {
  447. unsigned long addr;
  448. if (0 == (addr = mk_conf_addr(bus, devfn, where)))
  449. return PCIBIOS_DEVICE_NOT_FOUND;
  450. switch(size) {
  451. case 1:
  452. *value = __kernel_ldbu(*(vucp)addr);
  453. break;
  454. case 2:
  455. *value = __kernel_ldwu(*(vusp)addr);
  456. break;
  457. case 4:
  458. *value = *(vuip)addr;
  459. break;
  460. default:
  461. return PCIBIOS_FUNC_NOT_SUPPORTED;
  462. }
  463. return PCIBIOS_SUCCESSFUL;
  464. }
  465. static int
  466. marvel_write_config(struct pci_bus *bus, unsigned int devfn, int where,
  467. int size, u32 value)
  468. {
  469. unsigned long addr;
  470. if (0 == (addr = mk_conf_addr(bus, devfn, where)))
  471. return PCIBIOS_DEVICE_NOT_FOUND;
  472. switch (size) {
  473. case 1:
  474. __kernel_stb(value, *(vucp)addr);
  475. mb();
  476. __kernel_ldbu(*(vucp)addr);
  477. break;
  478. case 2:
  479. __kernel_stw(value, *(vusp)addr);
  480. mb();
  481. __kernel_ldwu(*(vusp)addr);
  482. break;
  483. case 4:
  484. *(vuip)addr = value;
  485. mb();
  486. *(vuip)addr;
  487. break;
  488. default:
  489. return PCIBIOS_FUNC_NOT_SUPPORTED;
  490. }
  491. return PCIBIOS_SUCCESSFUL;
  492. }
  493. struct pci_ops marvel_pci_ops =
  494. {
  495. .read = marvel_read_config,
  496. .write = marvel_write_config,
  497. };
  498. /*
  499. * Other PCI helper functions.
  500. */
  501. void
  502. marvel_pci_tbi(struct pci_controller *hose, dma_addr_t start, dma_addr_t end)
  503. {
  504. io7_ioport_csrs *csrs = ((struct io7_port *)hose->sysdata)->csrs;
  505. wmb();
  506. csrs->POx_SG_TBIA.csr = 0;
  507. mb();
  508. csrs->POx_SG_TBIA.csr;
  509. }
  510. /*
  511. * RTC Support
  512. */
  513. struct marvel_rtc_access_info {
  514. unsigned long function;
  515. unsigned long index;
  516. unsigned long data;
  517. };
  518. static void
  519. __marvel_access_rtc(void *info)
  520. {
  521. struct marvel_rtc_access_info *rtc_access = info;
  522. register unsigned long __r0 __asm__("$0");
  523. register unsigned long __r16 __asm__("$16") = rtc_access->function;
  524. register unsigned long __r17 __asm__("$17") = rtc_access->index;
  525. register unsigned long __r18 __asm__("$18") = rtc_access->data;
  526. __asm__ __volatile__(
  527. "call_pal %4 # cserve rtc"
  528. : "=r"(__r16), "=r"(__r17), "=r"(__r18), "=r"(__r0)
  529. : "i"(PAL_cserve), "0"(__r16), "1"(__r17), "2"(__r18)
  530. : "$1", "$22", "$23", "$24", "$25");
  531. rtc_access->data = __r0;
  532. }
  533. static u8
  534. __marvel_rtc_io(u8 b, unsigned long addr, int write)
  535. {
  536. static u8 index = 0;
  537. struct marvel_rtc_access_info rtc_access;
  538. u8 ret = 0;
  539. switch(addr) {
  540. case 0x70: /* RTC_PORT(0) */
  541. if (write) index = b;
  542. ret = index;
  543. break;
  544. case 0x71: /* RTC_PORT(1) */
  545. rtc_access.index = index;
  546. rtc_access.data = bcd2bin(b);
  547. rtc_access.function = 0x48 + !write; /* GET/PUT_TOY */
  548. __marvel_access_rtc(&rtc_access);
  549. ret = bin2bcd(rtc_access.data);
  550. break;
  551. default:
  552. printk(KERN_WARNING "Illegal RTC port %lx\n", addr);
  553. break;
  554. }
  555. return ret;
  556. }
  557. /*
  558. * IO map support.
  559. */
  560. void __iomem *
  561. marvel_ioremap(unsigned long addr, unsigned long size)
  562. {
  563. struct pci_controller *hose;
  564. unsigned long baddr, last;
  565. struct vm_struct *area;
  566. unsigned long vaddr;
  567. unsigned long *ptes;
  568. unsigned long pfn;
  569. /*
  570. * Adjust the address.
  571. */
  572. FIXUP_MEMADDR_VGA(addr);
  573. /*
  574. * Find the hose.
  575. */
  576. for (hose = hose_head; hose; hose = hose->next) {
  577. if ((addr >> 32) == (hose->mem_space->start >> 32))
  578. break;
  579. }
  580. if (!hose)
  581. return NULL;
  582. /*
  583. * We have the hose - calculate the bus limits.
  584. */
  585. baddr = addr - hose->mem_space->start;
  586. last = baddr + size - 1;
  587. /*
  588. * Is it direct-mapped?
  589. */
  590. if ((baddr >= __direct_map_base) &&
  591. ((baddr + size - 1) < __direct_map_base + __direct_map_size)) {
  592. addr = IDENT_ADDR | (baddr - __direct_map_base);
  593. return (void __iomem *) addr;
  594. }
  595. /*
  596. * Check the scatter-gather arena.
  597. */
  598. if (hose->sg_pci &&
  599. baddr >= (unsigned long)hose->sg_pci->dma_base &&
  600. last < (unsigned long)hose->sg_pci->dma_base + hose->sg_pci->size) {
  601. /*
  602. * Adjust the limits (mappings must be page aligned)
  603. */
  604. baddr -= hose->sg_pci->dma_base;
  605. last -= hose->sg_pci->dma_base;
  606. baddr &= PAGE_MASK;
  607. size = PAGE_ALIGN(last) - baddr;
  608. /*
  609. * Map it.
  610. */
  611. area = get_vm_area(size, VM_IOREMAP);
  612. if (!area)
  613. return NULL;
  614. ptes = hose->sg_pci->ptes;
  615. for (vaddr = (unsigned long)area->addr;
  616. baddr <= last;
  617. baddr += PAGE_SIZE, vaddr += PAGE_SIZE) {
  618. pfn = ptes[baddr >> PAGE_SHIFT];
  619. if (!(pfn & 1)) {
  620. printk("ioremap failed... pte not valid...\n");
  621. vfree(area->addr);
  622. return NULL;
  623. }
  624. pfn >>= 1; /* make it a true pfn */
  625. if (__alpha_remap_area_pages(vaddr,
  626. pfn << PAGE_SHIFT,
  627. PAGE_SIZE, 0)) {
  628. printk("FAILED to map...\n");
  629. vfree(area->addr);
  630. return NULL;
  631. }
  632. }
  633. flush_tlb_all();
  634. vaddr = (unsigned long)area->addr + (addr & ~PAGE_MASK);
  635. return (void __iomem *) vaddr;
  636. }
  637. /* Assume it was already a reasonable address */
  638. vaddr = baddr + hose->mem_space->start;
  639. return (void __iomem *) vaddr;
  640. }
  641. void
  642. marvel_iounmap(volatile void __iomem *xaddr)
  643. {
  644. unsigned long addr = (unsigned long) xaddr;
  645. if (addr >= VMALLOC_START)
  646. vfree((void *)(PAGE_MASK & addr));
  647. }
  648. int
  649. marvel_is_mmio(const volatile void __iomem *xaddr)
  650. {
  651. unsigned long addr = (unsigned long) xaddr;
  652. if (addr >= VMALLOC_START)
  653. return 1;
  654. else
  655. return (addr & 0xFF000000UL) == 0;
  656. }
  657. #define __marvel_is_port_kbd(a) (((a) == 0x60) || ((a) == 0x64))
  658. #define __marvel_is_port_rtc(a) (((a) == 0x70) || ((a) == 0x71))
  659. void __iomem *marvel_ioportmap (unsigned long addr)
  660. {
  661. FIXUP_IOADDR_VGA(addr);
  662. return (void __iomem *)addr;
  663. }
  664. unsigned int
  665. marvel_ioread8(void __iomem *xaddr)
  666. {
  667. unsigned long addr = (unsigned long) xaddr;
  668. if (__marvel_is_port_kbd(addr))
  669. return 0;
  670. else if (__marvel_is_port_rtc(addr))
  671. return __marvel_rtc_io(0, addr, 0);
  672. else if (marvel_is_ioaddr(addr))
  673. return __kernel_ldbu(*(vucp)addr);
  674. else
  675. /* this should catch other legacy addresses
  676. that would normally fail on MARVEL,
  677. because there really is nothing there...
  678. */
  679. return ~0;
  680. }
  681. void
  682. marvel_iowrite8(u8 b, void __iomem *xaddr)
  683. {
  684. unsigned long addr = (unsigned long) xaddr;
  685. if (__marvel_is_port_kbd(addr))
  686. return;
  687. else if (__marvel_is_port_rtc(addr))
  688. __marvel_rtc_io(b, addr, 1);
  689. else if (marvel_is_ioaddr(addr))
  690. __kernel_stb(b, *(vucp)addr);
  691. }
  692. #ifndef CONFIG_ALPHA_GENERIC
  693. EXPORT_SYMBOL(marvel_ioremap);
  694. EXPORT_SYMBOL(marvel_iounmap);
  695. EXPORT_SYMBOL(marvel_is_mmio);
  696. EXPORT_SYMBOL(marvel_ioportmap);
  697. EXPORT_SYMBOL(marvel_ioread8);
  698. EXPORT_SYMBOL(marvel_iowrite8);
  699. #endif
  700. /*
  701. * NUMA Support
  702. */
  703. /**********
  704. * FIXME - for now each cpu is a node by itself
  705. * -- no real support for striped mode
  706. **********
  707. */
  708. int
  709. marvel_pa_to_nid(unsigned long pa)
  710. {
  711. int cpuid;
  712. if ((pa >> 43) & 1) /* I/O */
  713. cpuid = (~(pa >> 35) & 0xff);
  714. else /* mem */
  715. cpuid = ((pa >> 34) & 0x3) | ((pa >> (37 - 2)) & (0x1f << 2));
  716. return marvel_cpuid_to_nid(cpuid);
  717. }
  718. int
  719. marvel_cpuid_to_nid(int cpuid)
  720. {
  721. return cpuid;
  722. }
  723. unsigned long
  724. marvel_node_mem_start(int nid)
  725. {
  726. unsigned long pa;
  727. pa = (nid & 0x3) | ((nid & (0x1f << 2)) << 1);
  728. pa <<= 34;
  729. return pa;
  730. }
  731. unsigned long
  732. marvel_node_mem_size(int nid)
  733. {
  734. return 16UL * 1024 * 1024 * 1024; /* 16GB */
  735. }
  736. /*
  737. * AGP GART Support.
  738. */
  739. #include <linux/agp_backend.h>
  740. #include <asm/agp_backend.h>
  741. #include <linux/slab.h>
  742. #include <linux/delay.h>
  743. struct marvel_agp_aperture {
  744. struct pci_iommu_arena *arena;
  745. long pg_start;
  746. long pg_count;
  747. };
  748. static int
  749. marvel_agp_setup(alpha_agp_info *agp)
  750. {
  751. struct marvel_agp_aperture *aper;
  752. if (!alpha_agpgart_size)
  753. return -ENOMEM;
  754. aper = kmalloc(sizeof(*aper), GFP_KERNEL);
  755. if (aper == NULL) return -ENOMEM;
  756. aper->arena = agp->hose->sg_pci;
  757. aper->pg_count = alpha_agpgart_size / PAGE_SIZE;
  758. aper->pg_start = iommu_reserve(aper->arena, aper->pg_count,
  759. aper->pg_count - 1);
  760. if (aper->pg_start < 0) {
  761. printk(KERN_ERR "Failed to reserve AGP memory\n");
  762. kfree(aper);
  763. return -ENOMEM;
  764. }
  765. agp->aperture.bus_base =
  766. aper->arena->dma_base + aper->pg_start * PAGE_SIZE;
  767. agp->aperture.size = aper->pg_count * PAGE_SIZE;
  768. agp->aperture.sysdata = aper;
  769. return 0;
  770. }
  771. static void
  772. marvel_agp_cleanup(alpha_agp_info *agp)
  773. {
  774. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  775. int status;
  776. status = iommu_release(aper->arena, aper->pg_start, aper->pg_count);
  777. if (status == -EBUSY) {
  778. printk(KERN_WARNING
  779. "Attempted to release bound AGP memory - unbinding\n");
  780. iommu_unbind(aper->arena, aper->pg_start, aper->pg_count);
  781. status = iommu_release(aper->arena, aper->pg_start,
  782. aper->pg_count);
  783. }
  784. if (status < 0)
  785. printk(KERN_ERR "Failed to release AGP memory\n");
  786. kfree(aper);
  787. kfree(agp);
  788. }
  789. static int
  790. marvel_agp_configure(alpha_agp_info *agp)
  791. {
  792. io7_ioport_csrs *csrs = ((struct io7_port *)agp->hose->sysdata)->csrs;
  793. struct io7 *io7 = ((struct io7_port *)agp->hose->sysdata)->io7;
  794. unsigned int new_rate = 0;
  795. unsigned long agp_pll;
  796. /*
  797. * Check the requested mode against the PLL setting.
  798. * The agpgart_be code has not programmed the card yet,
  799. * so we can still tweak mode here.
  800. */
  801. agp_pll = io7->csrs->POx_RST[IO7_AGP_PORT].csr;
  802. switch(IO7_PLL_RNGB(agp_pll)) {
  803. case 0x4: /* 2x only */
  804. /*
  805. * The PLL is only programmed for 2x, so adjust the
  806. * rate to 2x, if necessary.
  807. */
  808. if (agp->mode.bits.rate != 2)
  809. new_rate = 2;
  810. break;
  811. case 0x6: /* 1x / 4x */
  812. /*
  813. * The PLL is programmed for 1x or 4x. Don't go faster
  814. * than requested, so if the requested rate is 2x, use 1x.
  815. */
  816. if (agp->mode.bits.rate == 2)
  817. new_rate = 1;
  818. break;
  819. default: /* ??????? */
  820. /*
  821. * Don't know what this PLL setting is, take the requested
  822. * rate, but warn the user.
  823. */
  824. printk("%s: unknown PLL setting RNGB=%lx (PLL6_CTL=%016lx)\n",
  825. __func__, IO7_PLL_RNGB(agp_pll), agp_pll);
  826. break;
  827. }
  828. /*
  829. * Set the new rate, if necessary.
  830. */
  831. if (new_rate) {
  832. printk("Requested AGP Rate %dX not compatible "
  833. "with PLL setting - using %dX\n",
  834. agp->mode.bits.rate,
  835. new_rate);
  836. agp->mode.bits.rate = new_rate;
  837. }
  838. printk("Enabling AGP on hose %d: %dX%s RQ %d\n",
  839. agp->hose->index, agp->mode.bits.rate,
  840. agp->mode.bits.sba ? " - SBA" : "", agp->mode.bits.rq);
  841. csrs->AGP_CMD.csr = agp->mode.lw;
  842. return 0;
  843. }
  844. static int
  845. marvel_agp_bind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
  846. {
  847. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  848. return iommu_bind(aper->arena, aper->pg_start + pg_start,
  849. mem->page_count, mem->pages);
  850. }
  851. static int
  852. marvel_agp_unbind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
  853. {
  854. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  855. return iommu_unbind(aper->arena, aper->pg_start + pg_start,
  856. mem->page_count);
  857. }
  858. static unsigned long
  859. marvel_agp_translate(alpha_agp_info *agp, dma_addr_t addr)
  860. {
  861. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  862. unsigned long baddr = addr - aper->arena->dma_base;
  863. unsigned long pte;
  864. if (addr < agp->aperture.bus_base ||
  865. addr >= agp->aperture.bus_base + agp->aperture.size) {
  866. printk("%s: addr out of range\n", __func__);
  867. return -EINVAL;
  868. }
  869. pte = aper->arena->ptes[baddr >> PAGE_SHIFT];
  870. if (!(pte & 1)) {
  871. printk("%s: pte not valid\n", __func__);
  872. return -EINVAL;
  873. }
  874. return (pte >> 1) << PAGE_SHIFT;
  875. }
  876. struct alpha_agp_ops marvel_agp_ops =
  877. {
  878. .setup = marvel_agp_setup,
  879. .cleanup = marvel_agp_cleanup,
  880. .configure = marvel_agp_configure,
  881. .bind = marvel_agp_bind_memory,
  882. .unbind = marvel_agp_unbind_memory,
  883. .translate = marvel_agp_translate
  884. };
  885. alpha_agp_info *
  886. marvel_agp_info(void)
  887. {
  888. struct pci_controller *hose;
  889. io7_ioport_csrs *csrs;
  890. alpha_agp_info *agp;
  891. struct io7 *io7;
  892. /*
  893. * Find the first IO7 with an AGP card.
  894. *
  895. * FIXME -- there should be a better way (we want to be able to
  896. * specify and what if the agp card is not video???)
  897. */
  898. hose = NULL;
  899. for (io7 = NULL; (io7 = marvel_next_io7(io7)) != NULL; ) {
  900. struct pci_controller *h;
  901. vuip addr;
  902. if (!io7->ports[IO7_AGP_PORT].enabled)
  903. continue;
  904. h = io7->ports[IO7_AGP_PORT].hose;
  905. addr = (vuip)build_conf_addr(h, 0, PCI_DEVFN(5, 0), 0);
  906. if (*addr != 0xffffffffu) {
  907. hose = h;
  908. break;
  909. }
  910. }
  911. if (!hose || !hose->sg_pci)
  912. return NULL;
  913. printk("MARVEL - using hose %d as AGP\n", hose->index);
  914. /*
  915. * Get the csrs from the hose.
  916. */
  917. csrs = ((struct io7_port *)hose->sysdata)->csrs;
  918. /*
  919. * Allocate the info structure.
  920. */
  921. agp = kmalloc(sizeof(*agp), GFP_KERNEL);
  922. if (!agp)
  923. return NULL;
  924. /*
  925. * Fill it in.
  926. */
  927. agp->hose = hose;
  928. agp->private = NULL;
  929. agp->ops = &marvel_agp_ops;
  930. /*
  931. * Aperture - not configured until ops.setup().
  932. */
  933. agp->aperture.bus_base = 0;
  934. agp->aperture.size = 0;
  935. agp->aperture.sysdata = NULL;
  936. /*
  937. * Capabilities.
  938. *
  939. * NOTE: IO7 reports through AGP_STAT that it can support a read queue
  940. * depth of 17 (rq = 0x10). It actually only supports a depth of
  941. * 16 (rq = 0xf).
  942. */
  943. agp->capability.lw = csrs->AGP_STAT.csr;
  944. agp->capability.bits.rq = 0xf;
  945. /*
  946. * Mode.
  947. */
  948. agp->mode.lw = csrs->AGP_CMD.csr;
  949. return agp;
  950. }