1*df756a77Smsaitoh /* $NetBSD: ixdp425_pci.c,v 1.13 2019/01/09 07:49:22 msaitoh Exp $ */
2663ccee1Sichiro #define PCI_DEBUG
397106736Sichiro /*
497106736Sichiro * Copyright (c) 2003
597106736Sichiro * Ichiro FUKUHARA <ichiro@ichiro.org>.
697106736Sichiro * All rights reserved.
797106736Sichiro *
897106736Sichiro * Redistribution and use in source and binary forms, with or without
997106736Sichiro * modification, are permitted provided that the following conditions
1097106736Sichiro * are met:
1197106736Sichiro * 1. Redistributions of source code must retain the above copyright
1297106736Sichiro * notice, this list of conditions and the following disclaimer.
1397106736Sichiro * 2. Redistributions in binary form must reproduce the above copyright
1497106736Sichiro * notice, this list of conditions and the following disclaimer in the
1597106736Sichiro * documentation and/or other materials provided with the distribution.
1697106736Sichiro *
1797106736Sichiro * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
1897106736Sichiro * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1997106736Sichiro * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2097106736Sichiro * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
2197106736Sichiro * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2297106736Sichiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2397106736Sichiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2497106736Sichiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2597106736Sichiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2697106736Sichiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2797106736Sichiro * SUCH DAMAGE.
2897106736Sichiro */
2997106736Sichiro
3097106736Sichiro #include <sys/cdefs.h>
31*df756a77Smsaitoh __KERNEL_RCSID(0, "$NetBSD: ixdp425_pci.c,v 1.13 2019/01/09 07:49:22 msaitoh Exp $");
3297106736Sichiro
3397106736Sichiro /*
3497106736Sichiro * IXDP425 PCI interrupt support.
3597106736Sichiro */
3697106736Sichiro
3797106736Sichiro #include <sys/param.h>
3897106736Sichiro #include <sys/systm.h>
3997106736Sichiro #include <sys/device.h>
4097106736Sichiro
4197106736Sichiro #include <machine/autoconf.h>
42fea15f47Sdyoung #include <sys/bus.h>
4397106736Sichiro
4497106736Sichiro #include <evbarm/ixdp425/ixdp425reg.h>
4597106736Sichiro #include <evbarm/ixdp425/ixdp425var.h>
4697106736Sichiro
4797106736Sichiro #include <arm/xscale/ixp425reg.h>
4897106736Sichiro #include <arm/xscale/ixp425var.h>
4997106736Sichiro
5097106736Sichiro #include <dev/pci/pcidevs.h>
5197106736Sichiro #include <dev/pci/ppbreg.h>
5297106736Sichiro
53a184f1f4Sdyoung static int ixdp425_pci_intr_map(const struct pci_attach_args *,
54a184f1f4Sdyoung pci_intr_handle_t *);
55e58a356cSchristos static const char *ixdp425_pci_intr_string(void *, pci_intr_handle_t, char *, size_t);
56fb2c5211Sscw static const struct evcnt *ixdp425_pci_intr_evcnt(void *, pci_intr_handle_t);
57fb2c5211Sscw static void *ixdp425_pci_intr_establish(void *, pci_intr_handle_t, int,
58*df756a77Smsaitoh int (*func)(void *), void *, const char *);
59fb2c5211Sscw static void ixdp425_pci_intr_disestablish(void *, void *);
6097106736Sichiro
6197106736Sichiro void
ixp425_md_pci_init(struct ixp425_softc * sc)62fb2c5211Sscw ixp425_md_pci_init(struct ixp425_softc *sc)
6397106736Sichiro {
64fb2c5211Sscw pci_chipset_tag_t pc = &sc->ia_pci_chipset;
6508a4aba7Sskrll uint32_t reg;
66fb2c5211Sscw
67fb2c5211Sscw /*
68fb2c5211Sscw * PCI initialization
69fb2c5211Sscw */
70fb2c5211Sscw pc->pc_intr_v = sc;
7197106736Sichiro pc->pc_intr_map = ixdp425_pci_intr_map;
7297106736Sichiro pc->pc_intr_string = ixdp425_pci_intr_string;
7397106736Sichiro pc->pc_intr_evcnt = ixdp425_pci_intr_evcnt;
7497106736Sichiro pc->pc_intr_establish = ixdp425_pci_intr_establish;
7597106736Sichiro pc->pc_intr_disestablish = ixdp425_pci_intr_disestablish;
76fb2c5211Sscw
77fb2c5211Sscw /* PCI Reset Assert */
78fb2c5211Sscw reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOUTR);
79fb2c5211Sscw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOUTR, reg & ~(1U << GPIO_PCI_RESET));
80fb2c5211Sscw
81fb2c5211Sscw /* PCI Clock Disable */
82fb2c5211Sscw reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPCLKR);
83fb2c5211Sscw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPCLKR, reg & ~GPCLKR_MUX14);
84fb2c5211Sscw
85fb2c5211Sscw /*
86fb2c5211Sscw * set GPIO Direction
87fb2c5211Sscw * Output: PCI_CLK, PCI_RESET
88fb2c5211Sscw * Input: PCI_INTA, PCI_INTB, PCI_INTC, PCI_INTD
89fb2c5211Sscw */
90fb2c5211Sscw reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOER);
91fb2c5211Sscw reg &= ~(1U << GPIO_PCI_CLK);
92fb2c5211Sscw reg &= ~(1U << GPIO_PCI_RESET);
93fb2c5211Sscw reg |= ((1U << GPIO_PCI_INTA) | (1U << GPIO_PCI_INTB) |
94fb2c5211Sscw (1U << GPIO_PCI_INTC) | (1U << GPIO_PCI_INTD));
95fb2c5211Sscw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOER, reg);
96fb2c5211Sscw
97fb2c5211Sscw /* clear ISR */
98fb2c5211Sscw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPISR,
99fb2c5211Sscw (1U << GPIO_PCI_INTA) | (1U << GPIO_PCI_INTB) |
100fb2c5211Sscw (1U << GPIO_PCI_INTC) | (1U << GPIO_PCI_INTD));
101fb2c5211Sscw
102fb2c5211Sscw /* wait 1ms to satisfy "minimum reset assertion time" of the PCI spec */
103fb2c5211Sscw DELAY(1000);
104fb2c5211Sscw reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPCLKR);
105fb2c5211Sscw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPCLKR, reg |
106fb2c5211Sscw (0xf << GPCLKR_CLK0DC_SHIFT) | (0xf << GPCLKR_CLK0TC_SHIFT));
107fb2c5211Sscw
108fb2c5211Sscw /* PCI Clock Enable */
109fb2c5211Sscw reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPCLKR);
110fb2c5211Sscw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPCLKR, reg | GPCLKR_MUX14);
111fb2c5211Sscw
112fb2c5211Sscw /*
113fb2c5211Sscw * wait 100us to satisfy "minimum reset assertion time from clock stable
114fb2c5211Sscw * requirement of the PCI spec
115fb2c5211Sscw */
116fb2c5211Sscw DELAY(100);
117fb2c5211Sscw /* PCI Reset deassert */
118fb2c5211Sscw reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOUTR);
119fb2c5211Sscw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOUTR, reg | (1U << GPIO_PCI_RESET));
120fb2c5211Sscw
121fb2c5211Sscw /*
122fb2c5211Sscw * AHB->PCI address translation
123fb2c5211Sscw * PCI Memory Map allocation in 0x48000000 (64MB)
124fb2c5211Sscw * see. IXP425_PCI_MEM_HWBASE
125fb2c5211Sscw */
126fb2c5211Sscw PCI_CSR_WRITE_4(sc, PCI_PCIMEMBASE, 0x48494a4b);
127fb2c5211Sscw
128fb2c5211Sscw /*
129fb2c5211Sscw * PCI->AHB address translation
130fb2c5211Sscw * begin at the physical memory start + OFFSET
131fb2c5211Sscw */
132fb2c5211Sscw #define AHB_OFFSET 0x10000000UL
133fb2c5211Sscw PCI_CSR_WRITE_4(sc, PCI_AHBMEMBASE,
134fb2c5211Sscw (AHB_OFFSET & 0xFF000000) +
135fb2c5211Sscw ((AHB_OFFSET & 0xFF000000) >> 8) +
136fb2c5211Sscw ((AHB_OFFSET & 0xFF000000) >> 16) +
137fb2c5211Sscw ((AHB_OFFSET & 0xFF000000) >> 24) +
138fb2c5211Sscw 0x00010203);
139fb2c5211Sscw
140fb2c5211Sscw /* write Mapping registers PCI Configuration Registers */
141fb2c5211Sscw /* Base Address 0 - 3 */
142fb2c5211Sscw ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR0, AHB_OFFSET + 0x00000000);
143fb2c5211Sscw ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR1, AHB_OFFSET + 0x01000000);
144fb2c5211Sscw ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR2, AHB_OFFSET + 0x02000000);
145fb2c5211Sscw ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR3, AHB_OFFSET + 0x03000000);
146fb2c5211Sscw
147fb2c5211Sscw /* Base Address 4 */
148fb2c5211Sscw ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR4, 0xffffffff);
149fb2c5211Sscw
150fb2c5211Sscw /* Base Address 5 */
151fb2c5211Sscw ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR5, 0x00000000);
152fb2c5211Sscw
153fb2c5211Sscw /* assert some PCI errors */
154fb2c5211Sscw PCI_CSR_WRITE_4(sc, PCI_ISR, ISR_AHBE | ISR_PPE | ISR_PFE | ISR_PSE);
155fb2c5211Sscw
156fb2c5211Sscw /*
157fb2c5211Sscw * Set up byte lane swapping between little-endian PCI
158fb2c5211Sscw * and the big-endian AHB bus
159fb2c5211Sscw */
160fb2c5211Sscw PCI_CSR_WRITE_4(sc, PCI_CSR, CSR_IC | CSR_ABE | CSR_PDS);
161fb2c5211Sscw
162fb2c5211Sscw /*
163fb2c5211Sscw * Enable bus mastering and I/O,memory access
164fb2c5211Sscw */
165fb2c5211Sscw ixp425_pci_conf_reg_write(sc, PCI_COMMAND_STATUS_REG,
166fb2c5211Sscw PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
167fb2c5211Sscw PCI_COMMAND_MASTER_ENABLE);
16897106736Sichiro }
16997106736Sichiro
170fb2c5211Sscw void
ixp425_md_pci_conf_interrupt(pci_chipset_tag_t pc,int bus,int dev,int pin,int swiz,int * ilinep)171fb2c5211Sscw ixp425_md_pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin,
172fb2c5211Sscw int swiz, int *ilinep)
173fb2c5211Sscw {
174fb2c5211Sscw
175fb2c5211Sscw if (bus == 0)
176fb2c5211Sscw *ilinep = ((swiz + (dev + pin - 1)) & 3);
177fb2c5211Sscw else
178fb2c5211Sscw panic("ixp425_md_pci_conf_interrupt: unsupported bus number");
179fb2c5211Sscw }
180fb2c5211Sscw
1811a8b4d7eSichiro #define IXP425_MAX_DEV 4
1821a8b4d7eSichiro #define IXP425_MAX_LINE 4
183fb2c5211Sscw static int
ixdp425_pci_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * ihp)184a184f1f4Sdyoung ixdp425_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
18597106736Sichiro {
1861a8b4d7eSichiro static int ixp425_pci_table[IXP425_MAX_DEV][IXP425_MAX_LINE] =
1871a8b4d7eSichiro {
1881a8b4d7eSichiro {PCI_INT_A, PCI_INT_B, PCI_INT_C, PCI_INT_D},
1891a8b4d7eSichiro {PCI_INT_B, PCI_INT_C, PCI_INT_D, PCI_INT_A},
1901a8b4d7eSichiro {PCI_INT_C, PCI_INT_D, PCI_INT_A, PCI_INT_B},
1911a8b4d7eSichiro {PCI_INT_D, PCI_INT_A, PCI_INT_B, PCI_INT_C},
1921a8b4d7eSichiro };
1931a8b4d7eSichiro
19497106736Sichiro int pin = pa->pa_intrpin;
1951a8b4d7eSichiro int dev = pa->pa_device;
19697106736Sichiro
19797106736Sichiro #ifdef PCI_DEBUG
19897106736Sichiro void *v = pa->pa_pc;
19997106736Sichiro int line = pa->pa_intrline;
20097106736Sichiro pcitag_t intrtag = pa->pa_intrtag;
20197106736Sichiro
20297106736Sichiro printf("ixdp425_pci_intr_map: v=%p, tag=%08lx intrpin=%d line=%d dev=%d\n",
20397106736Sichiro v, intrtag, pin, line, dev);
20497106736Sichiro #endif
20597106736Sichiro
2061a8b4d7eSichiro if (pin >= 1 && pin <= IXP425_MAX_LINE &&
2071a8b4d7eSichiro dev >= 1 && dev <= IXP425_MAX_DEV) {
2081a8b4d7eSichiro *ihp = ixp425_pci_table[dev - 1][pin - 1];
20997106736Sichiro return (0);
2101a8b4d7eSichiro } else {
21197106736Sichiro printf("ixdp425_pci_intr_map: no mapping for %d/%d/%d\n",
21297106736Sichiro pa->pa_bus, pa->pa_device, pa->pa_function);
21397106736Sichiro return (1);
21497106736Sichiro }
21597106736Sichiro }
21697106736Sichiro
217fb2c5211Sscw static const char *
ixdp425_pci_intr_string(void * v,pci_intr_handle_t ih,char * buf,size_t len)218e58a356cSchristos ixdp425_pci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
21997106736Sichiro {
2200c5ccc02Sjmcneill snprintf(buf, len, "ixp425 irq %" PRIu64, ih);
221e58a356cSchristos return buf;
22297106736Sichiro }
22397106736Sichiro
224fb2c5211Sscw static const struct evcnt *
ixdp425_pci_intr_evcnt(void * v,pci_intr_handle_t ih)22597106736Sichiro ixdp425_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
22697106736Sichiro {
22797106736Sichiro return (NULL);
22897106736Sichiro }
22997106736Sichiro
230fb2c5211Sscw static void *
ixdp425_pci_intr_establish(void * v,pci_intr_handle_t ih,int ipl,int (* func)(void *),void * arg,const char * xname)23197106736Sichiro ixdp425_pci_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
232*df756a77Smsaitoh int (*func)(void *), void *arg, const char *xname)
23397106736Sichiro {
23497106736Sichiro #ifdef PCI_DEBUG
23597106736Sichiro printf("ixdp425_pci_intr_establish(v=%p, irq=%d, ipl=%d, func=%p, arg=%p)\n",
23697106736Sichiro v, (int) ih, ipl, func, arg);
23797106736Sichiro #endif
23897106736Sichiro
23997106736Sichiro return (ixp425_intr_establish(ih, ipl, func, arg));
24097106736Sichiro }
24197106736Sichiro
242fb2c5211Sscw static void
ixdp425_pci_intr_disestablish(void * v,void * cookie)24397106736Sichiro ixdp425_pci_intr_disestablish(void *v, void *cookie)
24497106736Sichiro {
24597106736Sichiro #ifdef PCI_DEBUG
24697106736Sichiro printf("ixdp425_pci_intr_disestablish(v=%p, cookie=%p)\n",
24797106736Sichiro v, cookie);
24897106736Sichiro #endif
24997106736Sichiro
25097106736Sichiro ixp425_intr_disestablish(cookie);
25197106736Sichiro }
252