spdmem_i2c.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* $OpenBSD: spdmem_i2c.c,v 1.1 2010/03/22 21:20:58 miod Exp $ */
  2. /* $NetBSD: spdmem.c,v 1.3 2007/09/20 23:09:59 xtraeme Exp $ */
  3. /*
  4. * Copyright (c) 2007 Jonathan Gray <jsg@openbsd.org>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * Copyright (c) 2007 Nicolas Joly
  20. * Copyright (c) 2007 Paul Goyette
  21. * Copyright (c) 2007 Tobias Nygren
  22. * All rights reserved.
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions
  26. * are met:
  27. * 1. Redistributions of source code must retain the above copyright
  28. * notice, this list of conditions and the following disclaimer.
  29. * 2. Redistributions in binary form must reproduce the above copyright
  30. * notice, this list of conditions and the following disclaimer in the
  31. * documentation and/or other materials provided with the distribution.
  32. * 3. The name of the author may not be used to endorse or promote products
  33. * derived from this software without specific prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
  36. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  37. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  39. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  40. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  41. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  42. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  43. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  44. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  45. * POSSIBILITY OF SUCH DAMAGE.
  46. */
  47. /*
  48. * Serial Presence Detect (SPD) memory identification
  49. */
  50. #include <sys/param.h>
  51. #include <sys/systm.h>
  52. #include <sys/device.h>
  53. #include <dev/spdmemvar.h>
  54. #include <dev/i2c/i2cvar.h>
  55. struct spdmem_iic_softc {
  56. struct spdmem_softc sc_base;
  57. i2c_tag_t sc_tag;
  58. i2c_addr_t sc_addr;
  59. };
  60. int spdmem_iic_match(struct device *, void *, void *);
  61. void spdmem_iic_attach(struct device *, struct device *, void *);
  62. uint8_t spdmem_iic_read(struct spdmem_softc *, uint8_t);
  63. struct cfattach spdmem_iic_ca = {
  64. sizeof(struct spdmem_iic_softc), spdmem_iic_match, spdmem_iic_attach
  65. };
  66. int
  67. spdmem_iic_match(struct device *parent, void *match, void *aux)
  68. {
  69. struct i2c_attach_args *ia = aux;
  70. struct spdmem_iic_softc sc;
  71. /* clever attachments like openfirmware informed macppc */
  72. if (strcmp(ia->ia_name, "spd") == 0)
  73. return (1);
  74. /* dumb, need sanity checks */
  75. if (strcmp(ia->ia_name, "eeprom") != 0)
  76. return (0);
  77. sc.sc_tag = ia->ia_tag;
  78. sc.sc_addr = ia->ia_addr;
  79. sc.sc_base.sc_read = spdmem_iic_read;
  80. return spdmem_probe(&sc.sc_base);
  81. }
  82. void
  83. spdmem_iic_attach(struct device *parent, struct device *self, void *aux)
  84. {
  85. struct spdmem_iic_softc *sc = (struct spdmem_iic_softc *)self;
  86. struct i2c_attach_args *ia = aux;
  87. sc->sc_tag = ia->ia_tag;
  88. sc->sc_addr = ia->ia_addr;
  89. sc->sc_base.sc_read = spdmem_iic_read;
  90. printf(":");
  91. spdmem_attach_common(&sc->sc_base);
  92. }
  93. uint8_t
  94. spdmem_iic_read(struct spdmem_softc *v, uint8_t reg)
  95. {
  96. struct spdmem_iic_softc *sc = (struct spdmem_iic_softc *)v;
  97. uint8_t val = 0xff;
  98. iic_acquire_bus(sc->sc_tag,0);
  99. iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  100. &reg, sizeof reg, &val, sizeof val, 0);
  101. iic_release_bus(sc->sc_tag, 0);
  102. return val;
  103. }