core_marvel.c 24 KB

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